From 1da873515416639583b7c6143646bbf064431657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Gr=C3=BCninger?= Date: Wed, 4 Nov 2026 23:38:47 +0100 Subject: [PATCH] Fix leak and crash when an allocation throws during parsing (#6023) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit json_sax_dom_parser::insert_key_with_index: own the new object_key_index in a unique_ptr before key_indexes may reallocate; a throwing reallocation leaked it. Signed-off-by: Christoph GrĂ¼ninger --- include/nlohmann/detail/input/json_sax.hpp | 3 ++- single_include/nlohmann/json.hpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 80850ab92..fc9e9b774 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -904,7 +904,8 @@ class json_sax_dom_parser : public json_sax_dom_error_state { hashes.push_back(hasher(element.first)); } - ordered_data.key_indexes.emplace_back(new object_key_index(std::move(hashes))); + std::unique_ptr index(new object_key_index(std::move(hashes))); // NOLINT(cppcoreguidelines-owning-memory,modernize-make-unique): std::make_unique is C++14 + ordered_data.key_indexes.push_back(std::move(index)); f.ordered.index = ordered_data.key_indexes.back().get(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bb81909ae..50089514f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16538,7 +16538,8 @@ class json_sax_dom_parser : public json_sax_dom_error_state { hashes.push_back(hasher(element.first)); } - ordered_data.key_indexes.emplace_back(new object_key_index(std::move(hashes))); + std::unique_ptr index(new object_key_index(std::move(hashes))); // NOLINT(cppcoreguidelines-owning-memory,modernize-make-unique): std::make_unique is C++14 + ordered_data.key_indexes.push_back(std::move(index)); f.ordered.index = ordered_data.key_indexes.back().get(); }