From: Florian Forster Date: Mon, 20 Nov 2017 09:43:10 +0000 (+0100) Subject: src/utils_format_json.c: Check return values of yajl_gen_* functions. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=419c41409a5c3ba5531008165dff12c6ef5cc819;p=collectd.git src/utils_format_json.c: Check return values of yajl_gen_* functions. CID: 179228, 179236, 179239 --- diff --git a/src/utils_format_json.c b/src/utils_format_json.c index 0a95a86d..9fb8f395 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -500,6 +500,14 @@ static int json_add_string(yajl_gen g, char const *str) /* {{{ */ } \ } while (0) +#define CHECK_SUCCESS(cmd) \ + do { \ + yajl_gen_status s = (cmd); \ + if (s != yajl_gen_status_ok) { \ + return (int)s; \ + } \ + } while (0) + static int format_json_meta(yajl_gen g, notification_meta_t *meta) /* {{{ */ { if (meta == NULL) @@ -525,7 +533,7 @@ static int format_json_meta(yajl_gen g, notification_meta_t *meta) /* {{{ */ default: ERROR("format_json_meta: unknown meta data type %d (name \"%s\")", meta->type, meta->name); - yajl_gen_null(g); + CHECK_SUCCESS(yajl_gen_null(g)); } return format_json_meta(g, meta->next); @@ -544,14 +552,14 @@ static int format_time(yajl_gen g, cdtime_t t) /* {{{ */ static int format_alert(yajl_gen g, notification_t const *n) /* {{{ */ { - yajl_gen_array_open(g); - yajl_gen_map_open(g); /* BEGIN alert */ + CHECK_SUCCESS(yajl_gen_array_open(g)); /* BEGIN array */ + CHECK_SUCCESS(yajl_gen_map_open(g)); /* BEGIN alert */ /* * labels */ JSON_ADD(g, "labels"); - yajl_gen_map_open(g); /* BEGIN labels */ + CHECK_SUCCESS(yajl_gen_map_open(g)); /* BEGIN labels */ JSON_ADD(g, "alertname"); if (strncmp(n->plugin, n->type, strlen(n->plugin)) == 0) @@ -577,36 +585,40 @@ static int format_alert(yajl_gen g, notification_t const *n) /* {{{ */ } JSON_ADD(g, "severity"); - JSON_ADD(g, (n->severity == NOTIF_FAILURE) - ? "FAILURE" - : (n->severity == NOTIF_WARNING) - ? "WARNING" - : (n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"); + JSON_ADD(g, + (n->severity == NOTIF_FAILURE) + ? "FAILURE" + : (n->severity == NOTIF_WARNING) + ? "WARNING" + : (n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"); JSON_ADD(g, "service"); JSON_ADD(g, "collectd"); - yajl_gen_map_close(g); /* END labels */ + CHECK_SUCCESS(yajl_gen_map_close(g)); /* END labels */ /* * annotations */ JSON_ADD(g, "annotations"); - yajl_gen_map_open(g); /* BEGIN annotations */ + CHECK_SUCCESS(yajl_gen_map_open(g)); /* BEGIN annotations */ JSON_ADD(g, "summary"); JSON_ADD(g, n->message); - if (format_json_meta(g, n->meta) != 0) + if (format_json_meta(g, n->meta) != 0) { return -1; + } - yajl_gen_map_close(g); /* END annotations */ + CHECK_SUCCESS(yajl_gen_map_close(g)); /* END annotations */ JSON_ADD(g, "startsAt"); - format_time(g, n->time); + if (format_time(g, n->time) != 0) { + return -1; + } - yajl_gen_map_close(g); /* END alert */ - yajl_gen_array_close(g); + CHECK_SUCCESS(yajl_gen_map_close(g)); /* END alert */ + CHECK_SUCCESS(yajl_gen_array_close(g)); /* END array */ return 0; } /* }}} format_alert */ @@ -671,7 +683,11 @@ int format_json_notification(char *buffer, size_t buffer_size, /* {{{ */ } /* copy to output buffer */ - yajl_gen_get_buf(g, &out, &unused_out_len); + if (yajl_gen_get_buf(g, &out, &unused_out_len) != yajl_gen_status_ok) { + yajl_gen_clear(g); + yajl_gen_free(g); + return -1; + } sstrncpy(buffer, (void *)out, buffer_size); yajl_gen_clear(g);