From 3ec8f4d3d019658e3047855b6b71436b60c6a3b4 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 8 Dec 2015 11:30:13 +0100 Subject: [PATCH] src/utils_format_json.c: Make sure "keys" is freed in all error paths. CID: 38008 --- src/utils_format_json.c | 46 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/utils_format_json.c b/src/utils_format_json.c index 48aa4fc6..8d46edd0 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -225,20 +225,15 @@ static int dsnames_to_json (char *buffer, size_t buffer_size, /* {{{ */ return (0); } /* }}} int dsnames_to_json */ -static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ - meta_data_t *meta) +static int meta_data_keys_to_json (char *buffer, size_t buffer_size, /* {{{ */ + meta_data_t *meta, char **keys, size_t keys_num) { size_t offset = 0; - char **keys = NULL; - int keys_num; int status; - int i; + size_t i; buffer[0] = 0; - if (meta == NULL) - return (EINVAL); - #define BUFFER_ADD(...) do { \ status = ssnprintf (buffer + offset, buffer_size - offset, \ __VA_ARGS__); \ @@ -250,13 +245,6 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ offset += ((size_t) status); \ } 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; @@ -298,10 +286,7 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ if (meta_data_get_boolean (meta, key, &value) == 0) BUFFER_ADD (",\"%s\":%s", key, value ? "true" : "false"); } - - free (key); } /* for (keys) */ - free (keys); if (offset <= 0) return (ENOENT); @@ -312,6 +297,31 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ #undef BUFFER_ADD return (0); +} /* }}} int meta_data_keys_to_json */ + +static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ + meta_data_t *meta) +{ + char **keys = NULL; + size_t keys_num; + int status; + size_t i; + + if ((buffer == NULL) || (buffer_size == 0) || (meta == NULL)) + return (EINVAL); + + status = meta_data_toc (meta, &keys); + if (status <= 0) + return (status); + keys_num = (size_t) status; + + status = meta_data_keys_to_json (buffer, buffer_size, meta, keys, keys_num); + + for (i = 0; i < keys_num; ++i) + sfree (keys[i]); + sfree (keys); + + return status; } /* }}} int meta_data_to_json */ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ -- 2.30.2