Skip to content

Fix allocation failure during JSON destruction#5239

Open
michaelsam94 wants to merge 1 commit into
nlohmann:developfrom
michaelsam94:codex/fix-destructor-allocation-5135
Open

Fix allocation failure during JSON destruction#5239
michaelsam94 wants to merge 1 commit into
nlohmann:developfrom
michaelsam94:codex/fix-destructor-allocation-5135

Conversation

@michaelsam94

Copy link
Copy Markdown

This PR fixes #5135 by preventing allocation failures from the temporary destruction stack from escaping basic_json's noexcept destructor.

The existing iterative destruction path is kept unchanged for the normal case. It is now wrapped with the library's JSON_TRY / JSON_CATCH macros; if growing the heap-allocated stack throws, destruction falls back to the regular array/object cleanup below instead of allowing the exception to cross the destructor boundary and call std::terminate.

A regression test in unit-regression2.cpp injects a one-shot global allocation failure while destroying a structured JSON value, verifying that the destructor consumes the failing stack allocation path and returns normally.

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate.

Local verification:

/usr/bin/c++ -std=c++11 -Iinclude -Itests/src -Itests/thirdparty/doctest tests/src/unit.cpp tests/src/unit-regression2.cpp -o build-local/unit-regression2 && ./build-local/unit-regression2 --test-case='*5135*'
./build-local/unit-regression2
/usr/bin/c++ -std=c++11 -Iinclude -Itests/src -Itests/thirdparty/doctest tests/src/unit.cpp tests/src/unit-allocator.cpp -o build-local/unit-allocator && ./build-local/unit-allocator
printf '#include <nlohmann/json.hpp>\nint main(){ nlohmann::json j = nlohmann::json::array({1,2,3}); return j.size() == 3 ? 0 : 1; }\n' | /usr/bin/c++ -std=c++11 -Isingle_include -x c++ - -o build-local/single-smoke && ./build-local/single-smoke
printf '#include <nlohmann/json.hpp>\nint main(){ nlohmann::json j = nlohmann::json::array({1,2,3}); return j.size() == 3 ? 0 : 1; }\n' | /usr/bin/c++ -std=c++11 -fno-exceptions -DJSON_NOEXCEPTION -Iinclude -x c++ - -o build-local/noexceptions-smoke && ./build-local/noexceptions-smoke
git diff --check

I could not run the full CMake suite locally because cmake is not available on this machine's PATH.

@michaelsam94 michaelsam94 requested a review from nlohmann as a code owner July 6, 2026 10:40
@nlohmann nlohmann added the please rebase Please rebase your branch to origin/develop label Jul 6, 2026
@nlohmann

nlohmann commented Jul 6, 2026

Copy link
Copy Markdown
Owner

I just merged a test which caused a conflict. Sorry, please rebase.

Signed-off-by: Michael Sam <michaelsam94@users.noreply.github.com>
@michaelsam94 michaelsam94 force-pushed the codex/fix-destructor-allocation-5135 branch from 877084d to d428f79 Compare July 6, 2026 10:59
@michaelsam94

Copy link
Copy Markdown
Author

Rebased onto current develop (acf076a) and resolved the test conflict by keeping the newly merged #4320 regression and placing the #5135 regression after it.\n\nRe-ran local checks:\n\n- unit-regression2: 9 test cases, 128 assertions passed\n- unit-allocator: 3 test cases, 17 assertions passed\n- single-header smoke compile\n- no-exceptions smoke compile\n- git diff --check

@nlohmann nlohmann removed the please rebase Please rebase your branch to origin/develop label Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

basic_json destructor allocates memory, violating noexcept semantics

2 participants