Skip to content

Fix swap(array_t&)/swap(object_t&) to update parent pointers under JSON_DIAGNOSTICS - #5464

Merged
nlohmann merged 1 commit into
developfrom
fix/swap-diagnostics-parents
Sep 15, 2026
Merged

nlohmann merged 1 commit into
developfrom
fix/swap-diagnostics-parents

Conversation

@nlohmann

@nlohmann nlohmann commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Summary

basic_json::swap(array_t& other) and basic_json::swap(object_t& other) swap the underlying container storage but never call set_parents(), unlike the generic swap(reference other) overload which does. This leaves m_parent pointers on the moved-in elements stale (pointing into the free-standing array_t/object_t, typically nullptr), which:

  • produces wrong JSON Pointer paths in exception messages when JSON_DIAGNOSTICS is enabled, and
  • can trip assert_invariant() on a subsequent copy in debug/assert-enabled builds.

swap(string_t&) and swap(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 what swap(reference other) already does.

Test plan

  • Added a regression test to tests/src/unit-diagnostics.cpp covering both overloads (checks the diagnostic path string and a subsequent copy-construction).
  • Verified the new test fails (wrong path / assertion) without the fix and passes with it.
  • Regenerated single_include/nlohmann/json.hpp via make amalgamate.

Breaking change?

No breaking changes. This only affects the diagnostic path string produced when JSON_DIAGNOSTICS=1 and one of these two swap() 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

@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Sep 5, 2026
@nlohmann
nlohmann force-pushed the fix/swap-diagnostics-parents branch 4 times, most recently from 25c4659 to 768a24c Compare September 11, 2026 15:56
@nlohmann nlohmann added 🚀 ready to merge Ready to merge - just waiting for CI to complete. and removed review needed It would be great if someone could review the proposed changes. labels Sep 11, 2026
@nlohmann nlohmann added this to the Release 3.13.0 milestone Sep 11, 2026
@nlohmann
nlohmann force-pushed the fix/swap-diagnostics-parents branch from 768a24c to f68a62e Compare September 14, 2026 07:17
…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
nlohmann force-pushed the fix/swap-diagnostics-parents branch from f68a62e to 19ec867 Compare September 14, 2026 19:21
@nlohmann
nlohmann merged commit 6487678 into develop Sep 15, 2026
159 checks passed
@nlohmann
nlohmann deleted the fix/swap-diagnostics-parents branch September 15, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

M 🚀 ready to merge Ready to merge - just waiting for CI to complete. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants