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
13 changes: 10 additions & 3 deletions include/boost/json/impl/array.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,17 @@ insert(
value_ref> init) ->
iterator
{
// the value_refs in init may point into this
// array, whose storage revert_insert can
// relocate and free, so buffer them first
array temp(init, sp_);
revert_insert r(
pos, init.size(), *this);
value_ref::write_array(
r.p, init, sp_);
pos, temp.size(), *this);
relocate(
r.p,
temp.data(),
temp.size());
temp.t_->size = 0;
return r.commit();
}

Expand Down
12 changes: 12 additions & 0 deletions test/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,18 @@ class array_test
BOOST_TEST(a[2].as_string() == str_);
BOOST_TEST(a[3].as_int64() == 3);
BOOST_TEST(a[4].as_int64() == 4);

// elements of init alias *this and
// insertion reallocates
array b({1, str_}, sp);
BOOST_TEST(b.capacity() == b.size());
b.insert(b.begin(), {b[0], b[1]});
BOOST_TEST(b.size() == 4);
BOOST_TEST(b[0].as_int64() == 1);
BOOST_TEST(b[1].as_string() == str_);
BOOST_TEST(b[2].as_int64() == 1);
BOOST_TEST(b[3].as_string() == str_);
check_storage(b, sp);
});

// emplace(const_iterator, arg)
Expand Down
Loading