Repository navigation
fix: keep LinkedList consistent when an element's drop panics - #124
Merged
Merged
Conversation
drop_nodes dropped the element before decrementing len, so a panicking destructor left len one too high and the drop guard then dropped the sentinel as an element. The sentinel was also leaked on that path. Move the element out and recycle the node first, and free the free list and the sentinel from the guard on both paths.
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.
LinkedList::drop_nodescalledptr::drop_in_placeon an element before decrementinglen. If that destructor panics,lenstays one too high, andDropGuard::drop(which runsdrop_nodesagain while unwinding) walks one node too far and drops the sentinel as if it were an element. The sentinel is also never freed on that path, although the comment says it is.This can't happen from Python today: the LRU stores
Handles, and dropping theirPyobjects only decrefs and never panics. It is a latent bug in the genericLinkedList<T>.I found it by running the module under Miri, which reports it deterministically:
Without Miri the same test may pass (here it did on its own and aborted the test binary when run with other tests), because what happens past the sentinel depends on whatever memory is there.
Change
drop_nodesmoves the element out, recycles the node and decrementslenfirst, and drops the element last, so a panic leaves the list consistent.DropGuardfrees the free list and the sentinel on both the normal and the panic path;LinkedList::dropjust runsdrop_nodesunder the guard.Test
A Rust unit test in
linked_list.rs: six elements, the third one panics on drop, and the list is dropped insidecatch_unwind.cargo test --no-default-features --lib: passes before and after on my machine (the UB is silent here).cargo +nightly miri test --no-default-features --lib(withPYO3_PYTHONset): UB atlinked_list.rs:224before; clean after, also with-Zmiri-tree-borrowsand-Zmiri-strict-provenance, with no leaks reported.The project's CI doesn't run
cargo testyet. If it's useful, I can send a small separate job forcargo testand Miri; in my fork the whole Miri job takes about a minute.