Fix swap(array_t&)/swap(object_t&) to update parent pointers under JSON_DIAGNOSTICS - #5464
Merged
Merged
Conversation
nlohmann
force-pushed
the
fix/swap-diagnostics-parents
branch
4 times, most recently
from
September 11, 2026 15:56
25c4659 to
768a24c
Compare
gregmarr
approved these changes
Sep 11, 2026
nlohmann
force-pushed
the
fix/swap-diagnostics-parents
branch
from
September 14, 2026 07:17
768a24c to
f68a62e
Compare
4 tasks
…ON_DIAGNOSTICS Both overloads swapped the underlying container storage but never called set_parents(), leaving elements moved into *this with stale m_parent pointers (typically nullptr from the free-standing array_t/object_t). This produced wrong JSON Pointer paths in diagnostic messages and could trip assert_invariant() on subsequent copies. Mirrors the fix already applied in swap(reference other). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
nlohmann
force-pushed
the
fix/swap-diagnostics-parents
branch
from
September 14, 2026 19:21
f68a62e to
19ec867
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
basic_json::swap(array_t& other)andbasic_json::swap(object_t& other)swap the underlying container storage but never callset_parents(), unlike the genericswap(reference other)overload which does. This leavesm_parentpointers on the moved-in elements stale (pointing into the free-standingarray_t/object_t, typicallynullptr), which:JSON_DIAGNOSTICSis enabled, andassert_invariant()on a subsequent copy in debug/assert-enabled builds.swap(string_t&)andswap(binary_t&)are unaffected since those value types have no children.Fix
Add a
set_parents();call after the existing swap in each overload, mirroring whatswap(reference other)already does.Test plan
tests/src/unit-diagnostics.cppcovering both overloads (checks the diagnostic path string and a subsequent copy-construction).single_include/nlohmann/json.hppviamake amalgamate.Breaking change?
No breaking changes. This only affects the diagnostic path string produced when
JSON_DIAGNOSTICS=1and one of these twoswap()overloads was previously used on a structured value with children — the path becomes correct instead of truncated. No public API signatures change.🤖 Generated with Claude Code