Fix insert() leaving an undestroyable container when a constructor throws - #75
Merged
Conversation
…rows make_uninitialized_space() shifts the elements at pos out of the way and immediately counts the new gap in size(). Until the caller constructs into that gap the container claims raw memory, so a throwing element constructor left it in a state that could not even be destroyed: the destructor ran over slots that were never built, or over slots the filling algorithm had already cleaned up, which is a double destroy. asan reports heap-use-after-free. All five callers now go through fill_uninitialized_space(), which closes the gap again if the fill throws. When relocating a T cannot throw, close_gap() shifts the tail back down and the container ends up exactly as it started. Otherwise it drops the tail, which is shorter but valid and destroys cleanly. std::move_if_noexcept is not an option there, a copy could throw a second exception during unwinding. That matches what the standard asks for: [sequence.reqmts] only requires the strong guarantee for a middle insert when moving cannot throw, and the basic guarantee otherwise. Closes #68 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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 #68.
make_uninitialized_space()shifts the elements atposout of the way and immediately counts the resulting gap insize(). Until the caller constructs into that gap the container claims raw memory, so a throwing element constructor left it in a state that could not even be destroyed:size()says 5 while slots 0 and 1 hold objectsstd::uninitialized_fill_nhad already destroyed on its way out.Fix
All five callers (
emplace, both singleinserts, the countinsert, the forward-iterator rangeinsert) now go throughfill_uninitialized_space(), which closes the gap again if the fill throws.close_gap()has two modes:std::move_if_noexcept, the usual way to preserve elements of a throwing-move type, is deliberately not used:close_gapruns mid-unwind, and a throwing copy there would be a second exception in flight andstd::terminate.That split matches the standard. [sequence.reqmts] only requires the strong guarantee for a middle insert when moving cannot throw, and the basic guarantee otherwise.
Tests
test/unit/insert_exception_safety.cppsweeps sizes 0-9 × every position × counts 1-5 × first/last throw point, acrossinsert(pos, count, v),insert(pos, first, last),insert(pos, v)andemplace, in both storage modes and across the reallocating boundary. For the nothrow-move type it asserts the contents are byte-identical to before; for the throwing-move type asan and the leak checker do the asserting.Reverting the guard reproduces the heap-use-after-free immediately.
Measured cost
Reviewed the codegen rather than assuming. For
svector<int, 8>thetry/catchis fully elided — identical.textsize (5851 bytes), no landing pads, no.gcc_except_tableentries. Forsvector<std::string, 8>the TU grows 3.9%, almost all of it one out-of-lineclose_gapperT— it is not templated on the lambda, so the five call sites share it.fill_uninitialized_spaceinlines away entirely at-O3.Not fixed here
make_uninitialized_space_new()has the same shape of bug on the growth path: if a move throws partway through relocating into the new storage, the elements already moved are leaked. Confirmed with asan and filed as #74, along with the deeper redesign that would remove this whole family of problems.