mirror of
https://github.com/json-c/json-c.git
synced 2026-11-07 06:00:16 +08:00
json_patch: fix move/copy false rejection between sibling keys sharing a prefix
json_patch_apply_move_copy() checks that "from" is not a proper prefix of "path" per RFC 6902 section 4.4 (a location cannot be moved into one of its own children), but it did so with a plain strncmp() over the raw pointer strings, without checking that the shared run of characters actually ends on a JSON Pointer reference-token boundary in "path". As a result, moving or copying between two sibling object keys where one name is a textual prefix of the other, e.g. "/aa" to "/aab", was rejected with "Invalid attempt to move parent under a child" even though "/aa" is not an ancestor pointer of "/aab" at all, just an unrelated key that happens to share a few leading characters. The fix requires that the character in "path" right after the shared "from" prefix be either the end of the string (equal paths, a no-op) or a '/' (a genuine nested reference token), before treating it as an attempt to move a location under its own descendant. Added two cases to json_patch_tests.json covering the move and copy operations between such sibling keys, and refreshed the corresponding golden test_json_patch.expected output.
This commit is contained in:
1 parent
25895a587e
commit
1b504e26ab
3 files changed
+24
-2
No files matched your search
@@ -370,6 +370,16 @@
|
||||
"patch": [{"op": "move", "from": "/foo", "path": "/foo"}],
|
||||
"expected": {"foo": 1} },
|
||||
|
||||
{ "comment": "Move between sibling keys sharing a name prefix is not a parent/child move",
|
||||
"doc": {"aa": 1, "aab": 2},
|
||||
"patch": [{"op": "move", "from": "/aa", "path": "/aab"}],
|
||||
"expected": {"aab": 1} },
|
||||
|
||||
{ "comment": "Copy between sibling keys sharing a name prefix is not a parent/child move",
|
||||
"doc": {"aa": 1, "aab": 2},
|
||||
"patch": [{"op": "copy", "from": "/aa", "path": "/aab"}],
|
||||
"expected": {"aa": 1, "aab": 1} },
|
||||
|
||||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
|
||||
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
|
||||
"expected": {"baz": [{"qux": "hello"}], "bar": 1} },
|
||||
|
||||
Reference in new issue
Block a user