From 4811f0b01b2876e805ee462b22701897535321f2 Mon Sep 17 00:00:00 2001 From: Jakob Olsson <jakob.olsson@iopsys.eu> Date: Thu, 3 Oct 2019 12:13:22 +0200 Subject: [PATCH] fix memory leaks --- api.c | 17 ++++++++++------- test/api_test.c | 7 +++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/api.c b/api.c index a77bd1e..27717e6 100644 --- a/api.c +++ b/api.c @@ -31,7 +31,7 @@ static char *get_file(const char *path) if (fseek(f, 0, SEEK_SET)) goto out_close; - buffer = calloc(1, len); + buffer = calloc(1, len + 1); if (!buffer) goto out_close; @@ -170,12 +170,13 @@ struct json_object *dummy_val(char *val, enum json_type type) return j_val; } -int add_array(struct json_object *ptr, int *idx, int len, char *val, enum json_type type, char *key) +int add_array(struct json_object *ptr, int *idx, int len, struct json_object *j_val, enum json_type type, char *key) { struct json_object *tmp, *tmp1; - struct json_object *j_val; - j_val = dummy_val(val, type); + if (len < 1) + return -1; + if (!j_val) return -1; json_object_object_get_ex(ptr, key, &tmp); @@ -220,6 +221,7 @@ int add_val(struct json_object *ptr, char *key, char *val, enum json_type type) j_val = dummy_val(val, type); if (!j_val || !json_object_is_type(j_val, type)) { fprintf(stderr, "Invalid input value, parsed value and input type does not match!\n"); + json_object_put(j_val); return -1; } @@ -231,12 +233,13 @@ int add_val(struct json_object *ptr, char *key, char *val, enum json_type type) ptr = json_object_new_object(); json_object_object_foreach(j_val, key1, val1) - json_object_object_add(ptr, key1, val1); + json_object_object_add(ptr, key1, json_object_get(val1)); if (key) { json_object_object_add(tmp, parsed_key, ptr); } + json_object_put(j_val); } else { - add_array(ptr, idx, len, val, type, parsed_key); + add_array(ptr, idx, len, j_val, type, parsed_key); } return 0; @@ -245,7 +248,7 @@ int add_val(struct json_object *ptr, char *key, char *val, enum json_type type) if (len < 1 || type == json_type_array) json_object_object_add(ptr, parsed_key, j_val); else { - add_array(ptr, idx, len, val, type, parsed_key); + add_array(ptr, idx, len, j_val, type, parsed_key); } return 0; diff --git a/test/api_test.c b/test/api_test.c index a2c9b13..a0bae12 100644 --- a/test/api_test.c +++ b/test/api_test.c @@ -15,6 +15,8 @@ static void test_cfg_parse_success(void **state) struct json_object *file = json_object_file_to_obj("test.json"); + assert_non_null(file); + struct json_object *obj = json_object_new_object(); struct json_object *tot = json_object_new_object(); @@ -43,6 +45,8 @@ static void test_build_from_scratch(void **state) struct json_object *file = json_object_file_to_obj("test.json"); struct json_object *jobj = NULL; + assert_non_null(file); + json_object_set_by_string(&jobj, NULL, "{\ \"test\":\"success\",\ @@ -71,6 +75,8 @@ static void test_build_from_scratch(void **state) printf("modify_obj=%s\n", json_object_get_string(jobj)); assert_int_equal(1, json_object_equal(file, jobj)); + json_object_put(jobj); + json_object_put(file); } static void test_json_set_object(void **state) @@ -511,6 +517,7 @@ static void test_json_write_to_file(void **state) obj = json_object_file_to_obj("cpy.json"); assert_int_equal(1, json_object_equal(file_obj, obj)); + json_object_put(obj); } int main(void) { -- GitLab