Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ value::set_at_pointer(
system::error_code& ec,
set_pointer_options const& opts )
{
// ref can refer to an element of this document, which the walk below
// can replace or relocate while it creates missing elements, so the new
// value is created before the document is modified
value new_value = ref.make_value( storage() );

value* result = detail::walk_pointer(
*this,
sv,
Expand Down Expand Up @@ -495,7 +500,7 @@ value::set_at_pointer(
});

if( result )
*result = ref.make_value( storage() );
*result = std::move(new_value);
return result;
}

Expand Down
8 changes: 8 additions & 0 deletions test/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ class pointer_test
BOOST_TEST(( jv == object{ {"x", 1} } ));
BOOST_TEST( *result == 1 );
BOOST_TEST( result == &jv.at_pointer("/x") );

// the new value has to be created before the walk, because the walk
// replaces and reallocates elements the value_ref can refer to
opts = {};
jv = object{ {"c", nullptr} };
result = &jv.set_at_pointer( "/c/x", jv.at("c"), opts );
BOOST_TEST(( jv == object{ {"c", object{ {"x", nullptr} }} } ));
BOOST_TEST( result == &jv.at_pointer("/c/x") );
Comment thread
grisumbras marked this conversation as resolved.
}

template<class ErrorCode>
Expand Down
Loading