]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
reject empty reference token as array index in json_pointer 949/head
authorJavid Khan <dxbjavid@gmail.com>
Mon, 20 Jul 2026 09:14:51 +0000 (14:44 +0530)
committerJavid Khan <dxbjavid@gmail.com>
Mon, 20 Jul 2026 09:14:51 +0000 (14:44 +0530)
json_pointer.c
tests/test_json_pointer.c
tests/test_json_pointer.expected

index f7bf696b21fec19a23aa22cd0ebe0af4f2588a85..9d7ad185f10ecd8b5db831fdaf1274dbd8b5c3f8 100644 (file)
@@ -46,6 +46,12 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur,
 static int is_valid_index(const char *path, size_t *idx)
 {
        size_t i, len = strlen(path);
+       /* an empty reference token is never a valid array index */
+       if (len == 0)
+       {
+               errno = EINVAL;
+               return 0;
+       }
        /* this code-path optimizes a bit, for when we reference the 0-9 index range
         * in a JSON array and because leading zeros not allowed
         */
index 09c195a2fbd63eb0dc34969ea10484e8b5f25e45..b18c98a95c01ad621b65c6778ac71dde15b0b4f3 100644 (file)
@@ -203,6 +203,10 @@ static void test_wrong_inputs_get(void)
        assert(0 != json_pointer_get(jo1, "/foo/01", NULL));
        assert(errno == EINVAL);
        errno = 0;
+       /* An empty reference token is not a valid array index */
+       assert(0 != json_pointer_get(jo1, "/foo/", NULL));
+       assert(errno == EINVAL);
+       errno = 0;
        assert(0 != json_pointer_getf(jo1, NULL, "/%s/a", "foo"));
        assert(errno == EINVAL);
        errno = 0;
@@ -314,6 +318,10 @@ static void test_wrong_inputs_set(void)
        printf("PASSED - SET - failed with invalid array index'\n");
        json_object_put(jo2);
 
+       assert(0 != json_pointer_set(&jo1, "/foo/", (jo2 = json_object_new_string("cod"))));
+       printf("PASSED - SET - failed with empty array index'\n");
+       json_object_put(jo2);
+
        jo2 = json_object_new_string("whatever");
        assert(0 != json_pointer_set(&jo1, "/fud/gaw", jo2));
        assert(0 == json_pointer_set(&jo1, "/fud", json_object_new_object()));
index 7cb158edf59b9574aa2431c01497f869ae2f83e1..3b363a5087395116f1706173e80f711e98b206bc 100644 (file)
@@ -36,4 +36,5 @@ PASSED - SET - failed with NULL params for input json & path
 PASSED - SET - failed 'cod' with path 'foo/bar'
 PASSED - SET - failed 'cod' with path 'foo/bar'
 PASSED - SET - failed with invalid array index'
+PASSED - SET - failed with empty array index'
 PASSED - SET - failed to set index to non-array