Repository navigation
Issue #58 - Insert the value of an element of the same container - #68
Open
nigels-com wants to merge 2 commits into
Open
nigels-com wants to merge 2 commits into
nigels-com wants to merge 2 commits into
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #58.
insert(pos, item)andrinsert(pos, item)move the existing elements to make a gap, and only then readitem. Ifitemrefers 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::vectorandstd::dequeguarantee thatv.insert(pos, v[i])inserts the valuev[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:insert(begin() + 1, cb[2])a c b c da b b c drinsert(begin() + 2, cb[0])a b a c da b b c dinsert(begin() + 2, cb[0])b a c db d c drinsert(begin() + 2, cb[3])a b d ca b a cThe
n-item formsinsert(pos, n, item)andrinsert(pos, n, item)have the same problem.Reading freed memory
Some calls first reallocate the storage that
itemis in, and then read it (heap-use-after-free under ASan):circular_buffer::resize(n, item)andrresize(n, item)whenn > capacity().circular_buffer_space_optimized:push_back,push_front, andinsert/rinsert(one item andnitems) whenever they grow the storage.resizeandrresizecall these, so they are affected too.Fix
A new internal helper,
is_stored(item), checks whetheritemis inside the buffer's storage. It compares addresses withstd::less, so the check is well-defined for unrelated pointers. Whenitemis stored, the function copies it to a local and calls itself with the copy. The shifting and reallocation code is unchanged.itemis an element of the container, so other calls cost the same as before.is_storedisprotectedand marked INTERNAL ONLY, socircular_buffer_space_optimizedcan call it before it reallocates.Left unchanged, following the standard:
assign(n, item):itemmust 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()intest/common.ippruns for both containers. It checks 16 cases:n-iteminsert/rinsertoverload, pluspush_backandpush_front;resizeandrresizebeyond the capacity.Each buffer is filled so that its first element isn't at the start of the storage.
The
n-item cases pass1u/2u. With anintcount and anintelement,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_testandspace_optimized_testpass with gcc and clang at C++03, C++11, C++17 and C++20, withBOOST_CB_ENABLE_DEBUGset to 0 and to 1.-Wall -Wextra -Wpedantic.