diff --git a/api.c b/api.c index a77bd1e524c6f0c03bb9af39a0599a45cf8bc2e1..27717e666466cb4c20b221a56cadb33e9dffcc3a 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 a2c9b13781a5be000bbc6b2e7c7dae0a74c6bfc6..a0bae12d36e950c7a18435cdc8271adf5209946b 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) {