Fix double-free when a comparator panics mid-split_off - #2
Draft
jonbaldie wants to merge 2 commits into
Draft
Conversation
A comparator that panics mid-split_off currently leaves the map with a stale length after a suffix has already been moved into a temporary right-hand tree. These tests assert that catching that panic must not leave length and reachable structure disagreeing. Co-authored-by: Jonathan Baldie <jonbaldie@users.noreply.github.com>
Root::split_off used to interleave Ord searches with move_suffix. A panic in the comparator after the first move left the map with a stale length and a partially detached tree, which consuming iteration could then double-free. All searches now complete before any key-value pair is relocated, so a panic in Ord leaves the original map unchanged. Ordinary (non-panicking) splits still walk the same edges in the same order. Co-authored-by: Jonathan Baldie <jonbaldie@users.noreply.github.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.
r? Darksonn
Root::split_offcurrently interleavessearch_node(which invokes the caller'sOrd/Borrowimpl and can panic) withmove_suffix, which physically relocates key-value pairs into a temporary right-hand tree one level at a time. If the comparator panics on a level after the firstmove_suffix, unwind drops that right-hand tree whileself.lengthis still the old, too-large value.IntoItertrusts that length and can double-free.This is rust-lang#158165 (
I-unsound). A comparator panic is allowed (e.g.RefCell::cmp); it must not become UB.Approach
Previous attempts (rust-lang#158710, rust-lang#160901) aborted after the first move. That is sound, but it cannot host the issue's
catch_unwindreproducer as a regression test (requested on rust-lang#158710), and it turns a recoverableRefCellpanic into a process abort.This change splits
split_offinto two passes:Ord. The tree is not mutated, so a panic leavesselfintact.move_suffix, then fix borders as before.Ordis not invoked here.Validation
len=12 reachable=6, thenfree(): double free detected.CAPACITY+1, min height 1), original insertion order, panic oncmp(6, 0). Height-0 (11 keys) already unwound cleanly.split_off:[(6, 6), (6, 0)]— first search at the root succeeds and moves the suffix; second search panics.len=12 reachable=12andinto_iter().nth_back(5)no longer double-frees../x.py test library/alloc: 336 unit tests passed (including the two new regression tests), plus the rest of the alloc suite.Fixes rust-lang#158165
AI/LLM disclosure