Skip to content

Commit 9d82826

Browse files
committed
add test checking that Vec push/pop does not invalidate pointers
1 parent 4680580 commit 9d82826

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/liballoc/tests/vec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,3 +1152,24 @@ fn test_try_reserve_exact() {
11521152
}
11531153

11541154
}
1155+
1156+
#[test]
1157+
fn test_stable_push_pop() {
1158+
// Test that, if we reserved enough space, adding and removing elements does not
1159+
// invalidate references into the vector (such as `v0`). This test also
1160+
// runs in Miri, which would detect such problems.
1161+
let mut v = Vec::with_capacity(10);
1162+
v.push(13);
1163+
1164+
// laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
1165+
let v0 = unsafe { &*(&v[0] as *const _) };
1166+
1167+
// Now do a bunch of things and occasionally use `v0` again to assert it is still valid.
1168+
v.push(1);
1169+
v.push(2);
1170+
v.insert(1, 1);
1171+
assert_eq!(*v0, 13);
1172+
v.remove(1);
1173+
v.pop().unwrap();
1174+
assert_eq!(*v0, 13);
1175+
}

0 commit comments

Comments
 (0)