Skip to content

Commit 8f479e3

Browse files
committed
fix aliasing in remove()
also add smoke test to detect relocation even in rustc runs
1 parent 4eacf45 commit 8f479e3

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/liballoc/tests/vec.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,8 @@ fn test_stable_pointers() {
13661366
v.push(13);
13671367

13681368
// Laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
1369-
let v0 = unsafe { &*(&v[0] as *const _) };
1369+
let v0 = &mut v[0];
1370+
let v0 = unsafe { &mut *(v0 as *mut _) };
13701371
// Now do a bunch of things and occasionally use `v0` again to assert it is still valid.
13711372

13721373
// Pushing/inserting and popping/removing
@@ -1420,6 +1421,10 @@ fn test_stable_pointers() {
14201421
assert_eq!(*v0, 13);
14211422
next_then_drop(v.splice(5..6, vec![1; 10].into_iter().filter(|_| true))); // lower bound not exact
14221423
assert_eq!(*v0, 13);
1424+
1425+
// Smoke test that would fire even outside Miri if an actual relocation happened.
1426+
*v0 -= 13;
1427+
assert_eq!(v[0], 0);
14231428
}
14241429

14251430
// https://github.com/rust-lang/rust/pull/49496 introduced specialization based on:

src/liballoc/vec.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ impl<T> Vec<T> {
12001200
} else {
12011201
unsafe {
12021202
self.len -= 1;
1203-
Some(ptr::read(self.get_unchecked(self.len())))
1203+
Some(ptr::read(self.as_ptr().add(self.len())))
12041204
}
12051205
}
12061206
}
@@ -2020,9 +2020,7 @@ where
20202020
let (lower, _) = iterator.size_hint();
20212021
let mut vector = Vec::with_capacity(lower.saturating_add(1));
20222022
unsafe {
2023-
// `vector` is new, cannot have aliases, so us getting exclusive references
2024-
// here is okay.
2025-
ptr::write(vector.get_unchecked_mut(0), element);
2023+
ptr::write(vector.as_mut_ptr(), element);
20262024
vector.set_len(1);
20272025
}
20282026
vector

0 commit comments

Comments
 (0)