Files
json/docs/mkdocs/docs/api/json_pointer/operator_slasheq.md
T
Niels Lohmann 091c10d4a5 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>
2026-10-28 20:23:55 +01:00

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)
  1. append another JSON pointer at the end of this JSON pointer
  2. append an unescaped reference token at the end of this JSON pointer
  3. 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

  1. JSON pointer with ptr appended
  2. JSON pointer with token appended without escaping token
  3. JSON pointer with array_idx appended

Exception safety

  1. 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.
  2. Strong guarantee: if an exception is thrown, there are no changes to the JSON pointer.
  3. Strong guarantee: if an exception is thrown, there are no changes to the JSON pointer.

Complexity

  1. Linear in the length of ptr.
  2. Amortized constant.
  3. 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 appending
  • push_back - append an unescaped token at the end of the pointer
  • JSON Pointer - the article on JSON Pointer support

Version history

  1. Added in version 3.6.0. Appending a JSON pointer to itself no longer results in undefined behavior in version 3.13.0.
  2. Added in version 3.6.0. Changed type of token to string_t in version 3.11.0.
  3. Added in version 3.6.0.