Skip to content
Open
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
6 changes: 6 additions & 0 deletions include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ value::set_at_pointer(
if( n >= opts.max_created_elements )
return nullptr;

// arr.size() + n + 1 equals index + 1; reject the value that
// would wrap so the resize count and the returned pointer stay
// in bounds
if( index == std::size_t(-1) )
return nullptr;

arr.resize( arr.size() + n + 1 );
}

Expand Down
20 changes: 20 additions & 0 deletions test/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ class pointer_test
BOOST_TEST( *result == 0 );
BOOST_TEST( result == &jv.at_pointer("/4") );

// an index equal to std::size_t(-1) makes the element count index + 1
// wrap to zero; the array must not be resized and no pointer past its
// end may be returned
opts = {};
opts.max_created_elements = std::size_t(-1);
jv = array{0};
{
std::string const ptr =
"/" + std::to_string(std::size_t(-1));
BOOST_TEST_THROWS_WITH_LOCATION( jv.set_at_pointer(ptr, 1, opts) );
BOOST_TEST(( jv == array{0} ));

system::error_code ec;
result = jv.set_at_pointer(ptr, 1, ec, opts);
BOOST_TEST( !result );
BOOST_TEST( ec == error::not_found );
BOOST_TEST( hasLocation(ec) );
BOOST_TEST(( jv == array{0} ));
}

opts = {};
opts.create_arrays = false;
opts.create_objects = false;
Expand Down
Loading