]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Fix memory leak in test_safe_json_pointer_set, the caller must free the passed value... master
authorEric Hawicz <erh+git@nimenees.com>
Fri, 20 Feb 2026 00:05:10 +0000 (19:05 -0500)
committerEric Hawicz <erh+git@nimenees.com>
Fri, 20 Feb 2026 00:05:10 +0000 (19:05 -0500)
tests/test_safe_json_pointer_set.c

index f96984b6765dcdfbd1d5cd2cdbe3896f3daa8874..cebe1faf8caa0f541d200eaf5705a9c4943256cf 100644 (file)
@@ -23,17 +23,17 @@ static const char *input_json_str = "{ "
 
 static void test_example_set_with_limit_index(void)
 {
-    struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
-    size_t limit_index = 10;
+       struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
+       size_t limit_index = 10;
 
-    // testing if json_pointer_set_with_limit_index() works as json_pointer_set()
-    assert(jo1 != NULL);
+       // testing if json_pointer_set_with_limit_index() works as json_pointer_set()
+       assert(jo1 != NULL);
        printf("PASSED - SET_WITH_LIMIT - LOADED TEST JSON\n");
        printf("%s\n", json_object_get_string(jo1));
 
-    assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/1", json_object_new_string("cod"), limit_index));
+       assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/1", json_object_new_string("cod"), limit_index));
        assert(0 == strcmp("cod", json_object_get_string(json_object_array_get_idx(
-                                     json_object_object_get(jo1, "foo"), 1))));
+                                     json_object_object_get(jo1, "foo"), 1))));
        printf("PASSED - SET_WITH_LIMIT - 'cod' in /foo/1\n");
        assert(0 != json_pointer_set_with_limit_index(&jo1, "/fud/gaw", (jo2 = json_tokener_parse("[1,2,3]")), limit_index));
        assert(errno == ENOENT);
@@ -79,31 +79,31 @@ static void test_example_set_with_limit_index(void)
        json_object_put(jo1);
        json_object_put(jo2);
 
-    // testing with limit_index
-    jo1 = json_tokener_parse("{'foo': ['bar', 'baz']}");
-    jo2 = json_tokener_parse("{'foo': ['bar', 'cod']}");
+       // testing with limit_index
+       jo1 = json_tokener_parse("{'foo': ['bar', 'baz']}");
+       jo2 = json_tokener_parse("{'foo': ['bar', 'cod']}");
 
-    assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/1", json_object_new_string("cod"), limit_index));
-    assert(json_object_equal(jo1, jo2));
-    printf("PASSED - SET_LIMIT - Set value within limit (/foo/1 with limit 10)\n");
+       assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/1", json_object_new_string("cod"), limit_index));
+       assert(json_object_equal(jo1, jo2));
+       printf("PASSED - SET_LIMIT - Set value within limit (/foo/1 with limit 10)\n");
 
-    assert(0 == json_pointer_set_with_limit_index(&jo1, "/bar", json_object_new_string("new_field"), limit_index));
-    printf("PASSED - SET_LIMIT - Set value on an object (limit is ignored)\n");
+       assert(0 == json_pointer_set_with_limit_index(&jo1, "/bar", json_object_new_string("new_field"), limit_index));
+       printf("PASSED - SET_LIMIT - Set value on an object (limit is ignored)\n");
 
-    assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/20", json_object_new_string("big_index"), (size_t)-1));
-    printf("PASSED - SET_LIMIT - Set value with limit_index = -1 (no limit)\n");
+       assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/20", json_object_new_string("big_index"), (size_t)-1));
+       printf("PASSED - SET_LIMIT - Set value with limit_index = -1 (no limit)\n");
 
-    json_object_put(jo1);
-    json_object_put(jo2);
+       json_object_put(jo1);
+       json_object_put(jo2);
 }
 
 static void test_wrong_inputs_set_with_limit_index(void)
 {
-    struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
-    size_t limit_index = 10;
+       struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
+       size_t limit_index = 10;
 
-    // testing if json_pointer_set_with_limit_index() works as json_pointer_set()
-    assert(jo1 != NULL);
+       // testing if json_pointer_set_with_limit_index() works as json_pointer_set()
+       assert(jo1 != NULL);
        printf("PASSED - SET_WITH_LIMIT - LOADED TEST JSON\n");
        printf("%s\n", json_object_get_string(jo1));
 
@@ -142,101 +142,108 @@ static void test_wrong_inputs_set_with_limit_index(void)
 
        json_object_put(jo1);
 
-    // testing with limit_index
-    jo1 = json_tokener_parse("{'foo': ['bar', 'baz']}");
+       // testing with limit_index
+       jo1 = json_tokener_parse("{'foo': ['bar', 'baz']}");
 
-    errno = 0;
-    jo2 = json_object_new_string("out_of_bounds");
-    assert(0 != json_pointer_set_with_limit_index(&jo1, "/foo/20", jo2, limit_index));
-    assert(errno == EINVAL);
-    printf("PASSED - SET_LIMIT - Failed to set index 20 with limit 10\n");
-    // The value object was not consumed, so we must put it.
-    json_object_put(jo2);
+       errno = 0;
+       jo2 = json_object_new_string("out_of_bounds");
+       assert(0 != json_pointer_set_with_limit_index(&jo1, "/foo/20", jo2, limit_index));
+       assert(errno == EINVAL);
+       printf("PASSED - SET_LIMIT - Failed to set index 20 with limit 10\n");
+       // The value object was not consumed, so we must put it.
+       json_object_put(jo2);
 
-    // corner case: setting an index that equals the limit (should be ok, as it's idx > max_idx)
-    errno = 0;
-    assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/10", json_object_new_string("at_the_limit"), limit_index));
-    printf("PASSED - SET_LIMIT - Succeeded to set index 10 with limit 10\n");
+       // corner case: setting an index that equals the limit (should be ok, as it's idx > max_idx)
+       errno = 0;
+       assert(0 == json_pointer_set_with_limit_index(&jo1, "/foo/10", json_object_new_string("at_the_limit"), limit_index));
+       printf("PASSED - SET_LIMIT - Succeeded to set index 10 with limit 10\n");
 
-    json_object_put(jo1);
+       json_object_put(jo1);
 }
 
 // callback for testing json_pointer_set_with_cb()
 static int test_cb_print_msg(json_object *parent, const char *key, size_t idx,
                             json_object *value, void *priv)
 {
-    printf("PASSED - SET_WITH_CB - This callback is called\n");
-    return 0;
+       printf("PASSED - SET_WITH_CB - This callback is called\n");
+       json_object_put(value);
+       return 0;
 }
 
 // callback for testing json_pointer_set_with_cb() with rejection logic
 static int test_cb_reject_logic(json_object *parent, const char *key, size_t idx,
                                json_object *value, void *priv)
 {
-    // Reject any operation if the key is "reject"
-    if (key && strcmp(key, "reject") == 0)
-    {
-        printf("PASSED - SET_WITH_CB - Callback correctly identified key 'reject' to reject\n");
-        return -1;
-    }
-    return 0;
+       // Reject any operation if the key is "reject"
+       if (key && strcmp(key, "reject") == 0)
+       {
+               printf("PASSED - SET_WITH_CB - Callback correctly identified key 'reject' to reject\n");
+               // Note: caller of json_pointer_set must free value
+               return -1;
+       }
+       json_object_put(value);
+       return 0;
 }
 
 static void test_set_with_cb(void)
 {
-    struct json_object *jo1 = json_tokener_parse(input_json_str);
-    size_t limit_index = 5;
+       struct json_object *jo2, *jo1 = json_tokener_parse(input_json_str);
+       size_t limit_index = 5;
 
        assert(jo1 != NULL);
        printf("PASSED - SET_WITH_CB - LOADED TEST JSON\n");
        printf("%s\n", json_object_get_string(jo1));
 
-    assert(0 == json_pointer_set_with_cb(&jo1, "/foo/1", json_object_new_string("cod"), test_cb_print_msg, 1, NULL));
-    printf("PASSED - SET_WITH_CB - callback test_cb_print_msg for /foo/1\n");
+
+       assert(0 == json_pointer_set_with_cb(&jo1, "/foo/1", json_object_new_string("cod"), test_cb_print_msg, 1, NULL));
+       printf("PASSED - SET_WITH_CB - callback test_cb_print_msg for /foo/1\n");
 
        assert(0 == json_pointer_set_with_cb(&jo1, "/foo/4", json_object_new_string("in"), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
-    printf("PASSED - SET_WITH_CB - callback json_object_array_put_with_idx_limit_cb for /foo/4 with limit_index 5\n");
+       printf("PASSED - SET_WITH_CB - callback json_object_array_put_with_idx_limit_cb for /foo/4 with limit_index 5\n");
 
-    assert(0 == json_pointer_set_with_cb(&jo1, "/foo/5", json_object_new_string("border"), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
-    printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/5 with limit_index 5\n");
+       assert(0 == json_pointer_set_with_cb(&jo1, "/foo/5", json_object_new_string("border"), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
+       printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/5 with limit_index 5\n");
 
-    assert(0 != json_pointer_set_with_cb(&jo1, "/foo/10", json_object_new_string("out"), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
-    assert(errno == EINVAL);
-    printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/10 with limit_index 5\n");
+       assert(0 != json_pointer_set_with_cb(&jo1, "/foo/10", (jo2 = json_object_new_string("out")), json_object_array_put_with_idx_limit_cb, 0, &limit_index));
+       json_object_put(jo2);
+       assert(errno == EINVAL);
+       printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb for /foo/10 with limit_index 5\n");
 
-    assert(0 != json_pointer_set_with_cb(&jo1, "/foo/2", json_object_new_string("null_priv"), json_object_array_put_with_idx_limit_cb, 0, NULL));
-    assert(errno == EFAULT);
-    printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb with NULL priv\n");
+       assert(0 != json_pointer_set_with_cb(&jo1, "/foo/2", (jo2 = json_object_new_string("null_priv")), json_object_array_put_with_idx_limit_cb, 0, NULL));
+       json_object_put(jo2);
+       assert(errno == EFAULT);
+       printf("PASSED - SET_WITH_CB - failed with callback json_object_array_put_with_idx_limit_cb with NULL priv\n");
 
        json_object_put(jo1);
 
        jo1 = json_tokener_parse("{'foo': 'bar'}");
-    assert(jo1 != NULL);
+       assert(jo1 != NULL);
 
        assert(0 == json_pointer_set_with_cb(&jo1, "/foo", json_object_new_string("cod"), test_cb_print_msg, 1, NULL));
-    printf("PASSED - SET_WITH_CB - cb_handles_obj=1: callback was triggered for object operation\n");
+       printf("PASSED - SET_WITH_CB - cb_handles_obj=1: callback was triggered for object operation\n");
 
        assert(0 == json_pointer_set_with_cb(&jo1, "/new_key", json_object_new_string("new_value"), test_cb_print_msg, 0, NULL));
-    printf("PASSED - SET_WITH_CB - cb_handles_obj=0: callback was NOT triggered for object operation, default logic was used\n");
-    json_object_put(jo1);
+       printf("PASSED - SET_WITH_CB - cb_handles_obj=0: callback was NOT triggered for object operation, default logic was used\n");
+       json_object_put(jo1);
 
        // testing rejection logic callback
-    jo1 = json_tokener_parse("{'data': {} }");
-    assert(jo1 != NULL);
+       jo1 = json_tokener_parse("{'data': {} }");
+       assert(jo1 != NULL);
 
-    assert(0 == json_pointer_set_with_cb(&jo1, "/data/accept", json_object_new_string("accepted_value"), test_cb_reject_logic, 1, NULL));
-    printf("PASSED - SET_WITH_CB - Rejection callback approved an allowed key\n");
+       assert(0 == json_pointer_set_with_cb(&jo1, "/data/accept", json_object_new_string("accepted_value"), test_cb_reject_logic, 1, NULL));
+       printf("PASSED - SET_WITH_CB - Rejection callback approved an allowed key\n");
 
-    assert(0 != json_pointer_set_with_cb(&jo1, "/data/reject", json_object_new_string("rejected_value"), test_cb_reject_logic, 1, NULL));
-    printf("PASSED - SET_WITH_CB - Rejection callback rejected a forbidden key\n");
+       assert(0 != json_pointer_set_with_cb(&jo1, "/data/reject", (jo2 = json_object_new_string("rejected_value")), test_cb_reject_logic, 1, NULL));
+       json_object_put(jo2);
+       printf("PASSED - SET_WITH_CB - Rejection callback rejected a forbidden key\n");
 
-    json_object_put(jo1);
+       json_object_put(jo1);
 }
 
 int main(int argc, char **argv)
 {
-    test_example_set_with_limit_index();
-    test_wrong_inputs_set_with_limit_index();
-    test_set_with_cb();
+       test_example_set_with_limit_index();
+       test_wrong_inputs_set_with_limit_index();
+       test_set_with_cb();
        return 0;
 }