From: Florian Forster Date: Tue, 2 Sep 2014 17:49:11 +0000 (+0200) Subject: src/utils_format_json.c: Handle empty meta_data_t gracefully. X-Git-Tag: collectd-5.3.2~46 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=7acb8df1c0fa0065f267d7f5af2abe260340900e;p=collectd.git src/utils_format_json.c: Handle empty meta_data_t gracefully. Previously, `meta != NULL` and `keys_num == 0` was possible and would result in "{}", which is not valid JSON. Fixes: 716 --- diff --git a/src/utils_format_json.c b/src/utils_format_json.c index bbc3dfdb..898b1725 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -234,7 +234,10 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ int status; int i; - memset (buffer, 0, buffer_size); + buffer[0] = 0; + + if (meta == NULL) + return (EINVAL); #define BUFFER_ADD(...) do { \ status = ssnprintf (buffer + offset, buffer_size - offset, \ @@ -248,6 +251,12 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ } while (0) keys_num = meta_data_toc (meta, &keys); + if (keys_num == 0) + { + sfree (keys); + return (0); + } + for (i = 0; i < keys_num; ++i) { int type; @@ -303,7 +312,7 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ #undef BUFFER_ADD return (0); -} /* int meta_data_to_json */ +} /* }}} int meta_data_to_json */ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ const data_set_t *ds, const value_list_t *vl, int store_rates)