* 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>
2.0 KiB
2.0 KiB
nlohmann::json_pointer::operator/=
// (1)
json_pointer& operator/=(const json_pointer& ptr);
// (2)
json_pointer& operator/=(string_t token);
// (3)
json_pointer& operator/=(std::size_t array_idx)
- append another JSON pointer at the end of this JSON pointer
- append an unescaped reference token at the end of this JSON pointer
- append an array index at the end of this JSON pointer
Parameters
ptr(in)- JSON pointer to append; may be the JSON pointer itself (
#!cpp p /= p), which then appends a copy of its reference tokens token(in)- reference token to append
array_idx(in)- array index to append
Return value
- JSON pointer with
ptrappended - JSON pointer with
tokenappended without escapingtoken - JSON pointer with
array_idxappended
Exception safety
- Basic guarantee: if an exception is thrown (for instance, if copying a reference token fails), the JSON pointer is
left in a valid state, but it may contain some of the reference tokens of
ptr. - Strong guarantee: if an exception is thrown, there are no changes to the JSON pointer.
- Strong guarantee: if an exception is thrown, there are no changes to the JSON pointer.
Complexity
- Linear in the length of
ptr. - Amortized constant.
- Amortized constant.
Examples
??? example
The example shows the usage of `operator/=`.
```cpp
--8<-- "examples/json_pointer__operator_add.cpp"
```
Output:
```json
--8<-- "examples/json_pointer__operator_add.output"
```
See also
operator/- create JSON Pointer by appendingpush_back- append an unescaped token at the end of the pointer- JSON Pointer - the article on JSON Pointer support
Version history
- Added in version 3.6.0. Appending a JSON pointer to itself no longer results in undefined behavior in version 3.13.0.
- Added in version 3.6.0. Changed type of
tokentostring_tin version 3.11.0. - Added in version 3.6.0.