From 9d82826e555e658680cf6ef246ae6d088f300d1d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 25 May 2019 10:11:00 +0200 Subject: [PATCH] add test checking that Vec push/pop does not invalidate pointers --- src/liballoc/tests/vec.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index 3307bdf94f9..5ddac673c9f 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -1152,3 +1152,24 @@ fn test_try_reserve_exact() { } } + +#[test] +fn test_stable_push_pop() { + // Test that, if we reserved enough space, adding and removing elements does not + // invalidate references into the vector (such as `v0`). This test also + // runs in Miri, which would detect such problems. + let mut v = Vec::with_capacity(10); + v.push(13); + + // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay. + let v0 = unsafe { &*(&v[0] as *const _) }; + + // Now do a bunch of things and occasionally use `v0` again to assert it is still valid. + v.push(1); + v.push(2); + v.insert(1, 1); + assert_eq!(*v0, 13); + v.remove(1); + v.pop().unwrap(); + assert_eq!(*v0, 13); +}