]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
require string type for patch op/path/from fields 946/head
authorJavid Khan <dxbjavid@gmail.com>
Sat, 18 Jul 2026 15:38:17 +0000 (21:08 +0530)
committerJavid Khan <dxbjavid@gmail.com>
Sat, 18 Jul 2026 15:38:17 +0000 (21:08 +0530)
json_patch.c
tests/json_patch_tests.json
tests/test_json_patch.expected

index 6b061ff51c39ae0ff94b5f12549259d2e01a5ed7..90e9d19e23f123dfda2876bcb27d457a8fea980e 100644 (file)
@@ -208,7 +208,7 @@ static int json_patch_apply_move_copy(struct json_object **res,
                return -1;
        }
 
-       from_s = json_object_get_string(jfrom);
+       from_s = json_object_get_type(jfrom) == json_type_string ? json_object_get_string(jfrom) : NULL;
        if (from_s == NULL) {
                _set_err(EINVAL, "Patch object 'from' field is not a string");
                return -1;
@@ -323,7 +323,7 @@ int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
                        _set_err(EINVAL, "Patch object does not contain 'op' field");
                        return -1;
                }
-               op = json_object_get_string(jop);
+               op = json_object_get_type(jop) == json_type_string ? json_object_get_string(jop) : NULL;
                if (op == NULL) {
                        _set_err(EINVAL, "Patch object 'op' field is not a string");
                        return -1;
@@ -332,7 +332,9 @@ int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
                        _set_err(EINVAL, "Patch object does not contain 'path' field");
                        return -1;
                }
-               path = json_object_get_string(jpath); // Note: empty string is ok!
+               // Note: empty string is ok!
+               path = json_object_get_type(jpath) == json_type_string ? json_object_get_string(jpath)
+                                                                      : NULL;
                if (path == NULL) {
                        _set_err(EINVAL, "Patch object 'path' field is not a string");
                        return -1;
index 10fad98df9f002ae481de84332395f170ab7868a..80103c0835c8db8a7f915d35328527507796653c 100644 (file)
        "doc": {"foo": "bar"},
        "patch": [{"op": "move", "from": null, "path": "/foo"}],
        "error": "a null from field should fail rather than crash"
+    },
+
+    { "comment": "null path field must be rejected, not dereferenced",
+       "doc": {"foo": "bar"},
+       "patch": [{"op": "remove", "path": null}],
+       "error": "a null path field should fail rather than crash"
     }
 
 ]
index 9f065e02d7ad23d3f08d629c2b83941be767c2cc..8882779ff458984415fac9ab775212a08c2946a0 100644 (file)
@@ -161,3 +161,5 @@ Testing 'null op field must be rejected, not dereferenced', doc '{ "foo": "bar"
  => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'op' field is not a string
 Testing 'null from field must be rejected, not dereferenced', doc '{ "foo": "bar" }' patch '[ { "op": "move", "from": null, "path": "\/foo" } ]' : OK
  => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'from' field is not a string
+Testing 'null path field must be rejected, not dereferenced', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": null } ]' : OK
+ => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'path' field is not a string