Fix double free when an item's Drop panics - #60
Merged
agerasev merged 1 commit intoSep 13, 2026
Merged
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().
agerasev
added a commit
that referenced
this pull request
Sep 13, 2026
PR #60 advances the read index before drop_in_place, allowing a concurrent producer to overwrite a slot while its destructor still accesses it. Its per-item loop also makes clear consume newly inserted items rather than finish after the original occupied snapshot. Use an unwind guard to count started destructors and publish the read index once, after the batch finishes or a panicking destructor has unwound. Keep clear delegated to skip while preserving bounded batch traversal. Extend regression coverage to actual clear calls, every panic position, wrapped storage, owned/split/frozen consumers, buffer reuse, and concurrent producer interactions. The new concurrency tests fail against the original PR; the panic test fails against the original master. Validation: scripts/test.sh and scripts/miri.sh pass; changed files pass rustfmt and git diff --check.
Owner
|
Thanks for the fix! And sorry for my late answer. I merged this with an unwind guard so slots become reusable only after destruction finishes. |
Contributor
Author
|
@agerasev |
Merged
4 tasks
taladar
added a commit
to taladar/sl-client
that referenced
this pull request
Sep 21, 2026
RUSTSEC-2026-0293: `Consumer::skip()` and `Consumer::clear()` drop the consumed elements before advancing the read index, so an element whose `Drop` panics leaves the index pointing at already-dropped slots and the ring buffer's own destructor frees them a second time. It reaches us through firewheel-core, under sl-audio and every crate stacked on it. Lockfile only; the fix is upstream in agerasev/ringbuf#60. The committed cargo-about output follows the version line it names. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01ETqA9spvoA4FRr4T2tNTRk Ran_pre-commit_check: frontend:::biome-format Ran_pre-commit_check: frontend:::biome-lint Ran_pre-commit_check: markdown:::code-blocks:::biome-format Ran_pre-commit_check: markdown:::code-blocks:::biome-lint Ran_pre-commit_check: markdown:::code-blocks:::prettier-css Ran_pre-commit_check: markdown:::code-blocks:::prettier-json Ran_pre-commit_check: markdown:::code-blocks:::rumdl Ran_pre-commit_check: markdown:::code-blocks:::rustfmt Ran_pre-commit_check: markdown:::code-blocks:::shellcheck-bash Ran_pre-commit_check: markdown:::code-blocks:::shellcheck-sh Ran_pre-commit_check: markdown:::code-blocks:::shfmt Ran_pre-commit_check: markdown:::code-blocks:::tombi Ran_pre-commit_check: markdown:::code-blocks:::tombi-fmt Ran_pre-commit_check: markdown:::rumdl Ran_pre-commit_check: python:::ruff-formatter Ran_pre-commit_check: python:::ruff-linter Ran_pre-commit_check: shell:::shellcheck Ran_pre-commit_check: shell:::shfmt Ran_pre-commit_check: toml:::tombi Ran_pre-commit_check: toml:::tombi-fmt Ran_pre-commit_check: xml:::xmllint Ran_pre-commit_check: xml:::xmllint-format Ran_pre-commit_check: common:::whitespace Ran_pre-commit_check: common:::non-ascii-filenames Ran_pre-commit_check: common:::typos Skipped_pre-commit_check: scripts/third-party-licenses.hbs;;;git-attribute;;;handlebars;;;whitespace-significant plain-text template Skipped_pre-commit_check: scripts/third-party-licenses.hbs;;;git-attribute;;;handlebars;;;whitespace-significant plain-text template Skipped_pre-commit_check: sl-lsl/tests/corpus/error/arg_type_mismatch.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/duplicate_function.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/missing_default_state.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/return_in_void.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/syntax_error.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/undefined_function.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/undefined_variable.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/error/wrong_arg_count.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/control_flow.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/duplicate_labels.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/events.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/functions_and_states.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/hello.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/implicit_conversions.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/lists.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/print_expression.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/scanner_leniency.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: sl-lsl/tests/corpus/valid/vector_component_comparisons.lsl;;;git-attribute;;;lsl:::lslint;;;deliberately invalid differential-oracle fixtures Skipped_pre-commit_check: viewer-assets/character/attentions.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/attentions.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/attentionsN.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/attentionsN.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/avatar_lad.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/avatar_lad.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/genepool.xml;;;git-attribute;;;xml;;;vendored upstream content Skipped_pre-commit_check: viewer-assets/character/genepool.xml;;;git-attribute;;;xml;;;vendored upstream content Ran_pre-commit_check: rust:::bin:::debian-changelog-includes-version Ran_pre-commit_check: rust:::cargo-about-outdated Ran_pre-commit_check: rust:::cargo-check Ran_pre-commit_check: rust:::cargo-deny-advisories Ran_pre-commit_check: rust:::cargo-deny-licenses Ran_pre-commit_check: rust:::cargo-deny-sources Ran_pre-commit_check: rust:::cargo-deny-workspace-member-deny-toml Ran_pre-commit_check: rust:::cargo-hack-feature-powerset Ran_pre-commit_check: rust:::cargo-lock-committed Ran_pre-commit_check: rust:::cargo-sort Ran_pre-commit_check: rust:::changelog-md-includes-version Ran_pre-commit_check: rust:::clippy Ran_pre-commit_check: rust:::crate-version-not-in-cargo-lock Ran_pre-commit_check: rust:::doc Ran_pre-commit_check: rust:::formatting Ran_pre-commit_check: rust:::lib:::semver-violations Ran_pre-commit_check: rust:::nextest Ran_pre-commit_check: rust:::unused-dependencies
This was referenced Sep 21, 2026
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.