Skip to content
Snippets Groups Projects
Commit 4811f0b0 authored by Jakob Olsson's avatar Jakob Olsson
Browse files

fix memory leaks

parent cbaec2e5
Branches
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment