Create the DOM parser's member value before placing it (#5985)

json_sax_dom_parser::set_member() constructed the new value in place over
the null member. basic_json(value_t) sets the type before the json_value
allocates, so a std::bad_alloc left a member typed object/array with a
null pointer, and the destructor crashed instead of propagating the
exception. Construct the value first and move it over the member (the
move constructor is noexcept).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-01 21:08:09 +01:00
1 parent ddc34f546a
commit c82bd8b39a
3 files changed
+157 -6

No files matched your search

+7 -3
View File
@@ -1301,9 +1301,13 @@ class json_sax_dom_parser : public json_sax_dom_error_state
{
if (JSON_HEDLEY_LIKELY(member.is_null()))
{
// a new member is null and holds nothing: construct the value
// over it instead of destroying and swapping
::new (static_cast<void*>(&member)) BasicJsonType(std::forward<Value>(v));
// a new member is null and holds nothing: move the value over it
// instead of destroying and swapping. The value is created first,
// because creating a container or a string can throw, and a
// constructor that was interrupted must not have touched the
// member (#5985); the move constructor is noexcept
BasicJsonType value(std::forward<Value>(v));
::new (static_cast<void*>(&member)) BasicJsonType(std::move(value));
}
else
{