Fix double free when an item's Drop panics - #60
Open
tooson9010-spec wants to merge 1 commit into
Open
Conversation
skip() and clear() dropped the occupied items in a loop and advanced the read index only afterwards. If T::drop panics the index update never happens, so the ring buffer's own Drop destroys the same items again. Advance the read index one slot at a time, before each drop_in_place. clear() now delegates to skip().
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.
skip()andclear()drop the occupied items in a loop and advance the readindex only afterwards. If an item's
Droppanics the index update neverhappens, so the ring buffer still treats the destroyed items as live and its own
Drop— which callsclear()— destroys them again.Reachable from safe Rust with any item type whose
Dropcan panic. Reportedearlier in #59.
Reproducer
The added test counts destructor calls on a 4-item buffer with one armed
Drop. On the current codecleardestroys more items than exist:With a heap-owning item type, AddressSanitizer reports
attempting double-freewhile the ring buffer's destructor runs.
The fix
Advance the read index one slot at a time, before each
drop_in_place, so apanicking
Dropleaves the item outside the occupied range.clear()nowdelegates to
skip()so the ordering rule lives in one place.This re-derives the occupied slices on every iteration, which costs more than
the single call the old loop made. If that matters here, a drop guard that keeps
the batch loop and fixes up the index on unwind would work too.
Regression test
src/tests/skip.rsgains a case that arms one item'sDropand asserts that noitem is destroyed more than once, for both
clearand a partialskip. Plaincargo testcatches it, no sanitizer needed; the rest of the suite passes.