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

// the resulting size, index + 1, would either exceed the
// array's size limit or wrap to 0 when index == size_t(-1)
if( index >= array::max_size() )
{
BOOST_JSON_FAIL( ec, error::array_too_large );
return nullptr;
Comment thread
grisumbras marked this conversation as resolved.
}

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::array_too_large );
BOOST_TEST( hasLocation(ec) );
BOOST_TEST(( jv == array{0} ));
}

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