Skip to content

Commit 1c0b2f4

Browse files
Gankraalexcrichton
authored andcommitted
properly null out ptr in LinkedList::split_off - fixes rust-lang#26020
1 parent 2cc6e8f commit 1c0b2f4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/libcollections/linked_list.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,13 @@ impl<T> LinkedList<T> {
616616
length: len - at
617617
};
618618

619+
// Swap split_node.next with list_head (which is None), nulling out split_node.next,
620+
// as it is the new tail.
619621
mem::swap(&mut split_node.resolve().unwrap().next, &mut splitted_list.list_head);
622+
// Null out list_head.prev. Note this `unwrap` won't fail because if at == len
623+
// we already branched out at the top of the fn to return the empty list.
624+
splitted_list.list_head.as_mut().unwrap().prev = Rawlink::none();
625+
// Fix the tail ptr
620626
self.list_tail = split_node;
621627
self.length = at;
622628

@@ -1082,6 +1088,26 @@ mod tests {
10821088
}
10831089
}
10841090

1091+
#[test]
1092+
fn test_26021() {
1093+
use std::iter::ExactSizeIterator;
1094+
// There was a bug in split_off that failed to null out the RHS's head's prev ptr.
1095+
// This caused the RHS's dtor to walk up into the LHS at drop and delete all of
1096+
// its nodes.
1097+
//
1098+
// https://github.com/rust-lang/rust/issues/26021
1099+
let mut v1 = LinkedList::new();
1100+
v1.push_front(1u8);
1101+
v1.push_front(1u8);
1102+
v1.push_front(1u8);
1103+
v1.push_front(1u8);
1104+
let _ = v1.split_off(3); // Dropping this now should not cause laundry consumption
1105+
assert_eq!(v1.len(), 3);
1106+
1107+
assert_eq!(v1.iter().len(), 3);
1108+
assert_eq!(v1.iter().collect::<Vec<_>>().len(), 3);
1109+
}
1110+
10851111
#[cfg(test)]
10861112
fn fuzz_test(sz: i32) {
10871113
let mut m: LinkedList<_> = LinkedList::new();

0 commit comments

Comments
 (0)