Skip to content

Commit

Permalink
src/utils_format_json.c: Handle empty meta_data_t gracefully.
Browse files Browse the repository at this point in the history
Previously, `meta != NULL` and `keys_num == 0` was possible and would
result in "{}", which is not valid JSON.

Fixes: 716
  • Loading branch information
octo committed Sep 2, 2014
1 parent 0f4dc42 commit 7acb8df
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/utils_format_json.c
Expand Up @@ -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, \
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7acb8df

Please sign in to comment.