Skip to content

Commit 43ceaa8

Browse files
committed
properly null out ptr in LinkedList::split_off - fixes #26020
1 parent dd81d1e commit 43ceaa8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcollections/linked_list.rs

+20
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ impl<T> LinkedList<T> {
610610
};
611611

612612
mem::swap(&mut split_node.resolve().unwrap().next, &mut splitted_list.list_head);
613+
splitted_list.list_head.as_mut().unwrap().prev = Rawlink::none();
613614
self.list_tail = split_node;
614615
self.length = at;
615616

@@ -1075,6 +1076,25 @@ mod tests {
10751076
}
10761077
}
10771078

1079+
#[test]
1080+
fn test_26021() {
1081+
// There was a bug in split_off that failed to null out the RHS's head's prev ptr.
1082+
// This caused the RHS's dtor to walk up into the LHS at drop and delete all of
1083+
// its nodes.
1084+
//
1085+
// https://github.com/rust-lang/rust/issues/26021
1086+
let mut v1 = LinkedList::new();
1087+
v1.push_front(1u8);
1088+
v1.push_front(1u8);
1089+
v1.push_front(1u8);
1090+
v1.push_front(1u8);
1091+
let _ = v1.split_off(3); // Dropping this now should not cause laundry consumption
1092+
assert_eq!(v1.len(), 3);
1093+
1094+
assert_eq!(v1.iter().len(), 3);
1095+
assert_eq!(v1.iter().collect::<Vec<_>>().len(), 3);
1096+
}
1097+
10781098
#[cfg(test)]
10791099
fn fuzz_test(sz: i32) {
10801100
let mut m: LinkedList<_> = LinkedList::new();

0 commit comments

Comments
 (0)