Fix double free when an element's Drop or a predicate panics - #2
Open
tooson9010-spec wants to merge 1 commit into
Open
Fix double free when an element's Drop or a predicate panics#2tooson9010-spec wants to merge 1 commit into
tooson9010-spec wants to merge 1 commit into
Conversation
clear and truncate destroy elements before committing the new length, and ArrayVec::retain_mut compacts with ptr::copy without the pre-loop guard the other retain_mut implementations already use. A panic in either place leaves a stale length covering destroyed or duplicated slots, and the container's own Drop revisits them. Commit the length first, and give ArrayVec::retain_mut the same guard as FastVecData::retain_mut.
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.
clearandtruncatedestroy the elements before committing the new length. IfT::droppanics the commit is skipped, so the container keeps a stale lengthcovering slots that were already destroyed, and its own
Dropdestroys themagain.
ArrayVec::retain_muthas a second path. It compacts withptr::copy, whichleaves the source slot as a duplicate, and commits
self.lenonly after the loop— without the
self.len = 0; // Ensure safety if panickedguard thatFastVecData::retain_mutalready uses. A panicking predicate is enough here; noelement
Drophas to panic.Reported in #1. Affected:
clearandtruncateon all three types, plusArrayVec::retain_mut(andretain, which delegates to it).The added tests count destructor calls on a 4-element vec with one armed
Drop.On the current code every element is destroyed twice:
The fix commits the length before the destroying loop, and gives
ArrayVec::retain_mutthe same guardFastVecData::retain_mutuses.SmallVeckeeps its
MARKERbit.One regression test per module, gated on
feature = "std".cargo test --features stdcatches it, no sanitizer needed; the rest of the suite passes ondefault,
--features stdand--all-features.