Skip to content

Issue #58 - Insert the value of an element of the same container - #68

Open
nigels-com wants to merge 2 commits into
boostorg:developfrom
nigels-com:insert-fix
Open

nigels-com wants to merge 2 commits into
boostorg:developfrom
nigels-com:insert-fix

Conversation

@nigels-com

Copy link
Copy Markdown
Collaborator

Fixes #58.

insert(pos, item) and rinsert(pos, item) move the existing elements to make a gap, and only then read item. If item refers to one of the elements that moved, the value inserted is whichever element has since moved into its slot, not the value the caller passed.

std::vector and std::deque guarantee that v.insert(pos, v[i]) inserts the value v[i] had at the time of the call. Code ported from them, or generic code that inserts one of a container's own elements, gets the wrong contents here, and nothing warns about it.

Starting from a b c d:

Call Expected Before this PR
insert(begin() + 1, cb[2]) a c b c d a b b c d
rinsert(begin() + 2, cb[0]) a b a c d a b b c d
full: insert(begin() + 2, cb[0]) b a c d b d c d
full: rinsert(begin() + 2, cb[3]) a b d c a b a c

The n-item forms insert(pos, n, item) and rinsert(pos, n, item) have the same problem.

Reading freed memory

Some calls first reallocate the storage that item is in, and then read it (heap-use-after-free under ASan):

  • circular_buffer::resize(n, item) and rresize(n, item) when n > capacity().
  • In circular_buffer_space_optimized: push_back, push_front, and insert/rinsert (one item and n items) whenever they grow the storage. resize and rresize call these, so they are affected too.

Fix

A new internal helper, is_stored(item), checks whether item is inside the buffer's storage. It compares addresses with std::less, so the check is well-defined for unrelated pointers. When item is stored, the function copies it to a local and calls itself with the copy. The shifting and reallocation code is unchanged.

  • The extra copy is made only when item is an element of the container, so other calls cost the same as before.
  • is_stored is protected and marked INTERNAL ONLY, so circular_buffer_space_optimized can call it before it reallocates.

Left unchanged, following the standard:

  • Rvalue overloads: an implementation may assume an rvalue argument isn't one of its own elements.
  • assign(n, item): item must not be a reference into the container.
  • insert(pos, first, last): [first, last) must not come from the container.

Tests

insert_element_of_itself_test() in test/common.ipp runs for both containers. It checks 16 cases:

  • every single-item and n-item insert/rinsert overload, plus push_back and push_front;
  • a buffer that isn't full and one that is full, where the referenced element may be the one overwritten;
  • resize and rresize beyond the capacity.

Each buffer is filled so that its first element isn't at the start of the storage.

The n-item cases pass 1u/2u. With an int count and an int element, insert(pos, 1, cb[3]) matches the iterator-range template. Its integral branch copies the item, which would hide the bug.

Without the fix: 10 checks fail in each test program, and ASan reports a heap-use-after-free in space_optimized_test.

With the fix:

  • base_test and space_optimized_test pass with gcc and clang at C++03, C++11, C++17 and C++20, with BOOST_CB_ENABLE_DEBUG set to 0 and to 1.
  • No new warnings with -Wall -Wextra -Wpedantic.
  • Clean under ASan and UBSan with clang at C++03 and C++11, debug on and off.

insert(pos, item) and rinsert(pos, item), and the n-item forms, moved the
existing elements before reading item. When item referred to one of those
elements, the inserted value was its neighbour's. resize() and rresize()
above the capacity, and the inserts and push_back/push_front of
circular_buffer_space_optimized, reallocated first and read freed memory.

Copy item first when it lies inside the storage, as std::vector does.
@nigels-com nigels-com self-assigned this Dec 3, 2026
@nigels-com nigels-com added the bug label Dec 3, 2026
@nigels-com nigels-com added this to the boost 1.94.0 milestone Dec 3, 2026
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@994ef5b). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop      #68   +/-   ##
==========================================
  Coverage           ?   99.44%           
==========================================
  Files              ?       10           
  Lines              ?     4691           
  Branches           ?     3415           
==========================================
  Hits               ?     4665           
  Misses             ?       23           
  Partials           ?        3           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

In MSVC Debug builds, a failed assert or an abort() opens a dialog. On CI
nobody dismisses it, so the test hangs until ctest times out and the
message is lost. Send CRT assert and error reports to stderr, and skip the
abort() fault report, so the test fails with its message instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: insert/rinsert store the wrong value when it refers to an element being shifted

1 participant