json_pointer: find null array elements

json_pointer_get_single_path() returned ENOENT when the array element
at an in-range index was NULL, which is how json-c stores a JSON null.
A null object member was already found, but on [0, null, 2] "/1" failed
with "not found", although RFC 6901 says it references the null value.

The array index is bounds-checked just before, so a NULL element now
resolves like a null member: json_pointer_get() returns 0 and sets
*res to NULL. This also fixes json_patch_apply(), which failed every
"test", "replace" and "remove" on a null array element, and every
"move" and "copy" from one, with "Did not find element referenced by
path field".

Added a json_pointer_get() test, and json_patch_tests.json cases that
mirror the existing null object member ones.
This commit is contained in:
Wleeaf authored and Eric Hawicz committed 2026-10-26 00:39:03 -04:00
1 parent 86fe287188
commit d9b375ff1b
5 files changed
+47 -10

No files matched your search

+12
View File
@@ -122,6 +122,18 @@ static void test_example_get(void)
test_example_int(jo1, json_pointers[i].s, json_pointers[i].i);
json_object_put(jo1);
/* Test /1 == null, a null array element is found and returned as NULL */
jo1 = json_tokener_parse("[0, null, 2]");
jo2 = jo1;
assert(0 == json_pointer_get(jo1, "/1", NULL));
assert(0 == json_pointer_get(jo1, "/1", &jo2));
assert(NULL == jo2);
errno = 0;
assert(0 != json_pointer_get(jo1, "/1/foo", NULL));
assert(errno == ENOENT);
printf("PASSED - GET - /1 == null\n");
json_object_put(jo1);
}
/* I'm not too happy with the RFC example to test the recursion of the json_pointer_get() function */