mirror of
https://github.com/nlohmann/json.git
synced 2026-10-31 09:55:42 +00:00
Fix self-aliasing in json_pointer::operator/= and patch_inplace (#5936)
* Fix self-aliasing in json_pointer::operator/= and patch_inplace json_pointer::operator/=(const json_pointer&) passed iterators into its own token vector to insert() when a pointer was appended to itself (p /= p), which is undefined behavior; libstdc++ produced "/a/b//" for "/a/b". The pointer now appends a copy in that case. basic_json::patch_inplace iterated the patch while changing *this, so j.patch_inplace(j) read freed memory. It now applies a copy of the patch when the patch is the value itself. A patch stored inside the value remains undefined behavior and is documented as such; copying the patch unconditionally would double the time of a large patch. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix JSON_DIAGNOSTICS test crash and clang-tidy hit in patch_inplace self-aliasing The spare-capacity test reallocated non-empty elements through get_ref(), which leaves stale parent pointers under JSON_DIAGNOSTICS; reserve while the array is empty instead. Suppress performance-unnecessary-copy-initialization for the copy that is needed because *this is modified. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Detect a patch stored inside the patched value with JSON_DIAGNOSTICS patch_inplace applied a copy only when the patch was the value itself. With JSON_DIAGNOSTICS the parent pointers make it cheap to also detect a patch nested inside the value, so that case is handled the same way. Without diagnostics it stays documented undefined behavior. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Silence misc-const-correctness for patch_aliases_this Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix clang-tidy findings in the operator/= self-append test Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
2120927a7a
commit
091c10d4a5
7 files changed
+234
-2
No files matched your search
@@ -917,6 +917,46 @@ TEST_CASE("JSON pointers")
|
||||
CHECK(ptr.to_string() == "/object/~1");
|
||||
}
|
||||
|
||||
SECTION("appending a pointer to itself")
|
||||
{
|
||||
// operator/= must not pass iterators into its own token vector to
|
||||
// insert(); each pointer is appended to itself and must yield the
|
||||
// pointer whose string representation is the original one twice
|
||||
const std::vector<std::string> pointers =
|
||||
{
|
||||
"",
|
||||
"/",
|
||||
"/a",
|
||||
"/a/b",
|
||||
"/foo/0/-",
|
||||
"/a~1b/~0c/~01",
|
||||
"/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17"
|
||||
};
|
||||
|
||||
for (const auto& s : pointers)
|
||||
{
|
||||
CAPTURE(s)
|
||||
json::json_pointer ptr(s);
|
||||
const json::json_pointer expected(s + s);
|
||||
|
||||
const json::json_pointer& ret = (ptr /= ptr);
|
||||
CHECK(&ret == &ptr);
|
||||
CHECK(ptr == expected);
|
||||
CHECK(ptr.to_string() == s + s);
|
||||
|
||||
// appending to itself again doubles the pointer once more
|
||||
ptr /= ptr;
|
||||
std::string quadruple = s + s;
|
||||
quadruple += quadruple;
|
||||
CHECK(ptr.to_string() == quadruple);
|
||||
|
||||
// operator/ appends to a copy and leaves the operand unchanged
|
||||
const json::json_pointer original(s);
|
||||
CHECK((original / original) == expected);
|
||||
CHECK(original.to_string() == s);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("equality comparison")
|
||||
{
|
||||
std::string ptr_string{"/foo/bar"};
|
||||
|
||||
Reference in new issue
Block a user