Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
String::push_str invalidates interior references even when it does not reallocate #70301
Comments
Yeah this looks pretty similar. Looks like fn main() {
let mut v = Vec::with_capacity(10);
v.push(0);
let v0 = unsafe { &*(&v[0] as *const _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
v.extend_from_slice(&[1]);
let _val = *v0;
} |
The problem is in this line: Line 2125 in 8ff7850 The |
It shouldn't be too hard to locally fix this, but that duplicates some code and there are some other uses of |
Here's a possible fix: diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index 4769091183a..fd2b336eceb 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -2121,8 +2121,9 @@ where
self.reserve(slice.len());
unsafe {
let len = self.len();
+ let dst_slice = slice::from_raw_parts_mut(self.as_mut_ptr().add(len), slice.len());
+ dst_slice.copy_from_slice(slice);
self.set_len(len + slice.len());
- self.get_unchecked_mut(len..).copy_from_slice(slice);
}
}
} |
Fix PR is up: #70558 |
Fix some aliasing issues in Vec `Vec::extend` and `Vec::truncate` invalidated references into the vector even without reallocation, because they (implicitly) created a mutable reference covering the *entire* initialized part of the vector. Fixes rust-lang#70301 I verified the fix by adding some new tests to Miri: rust-lang/miri#1253
Fix some aliasing issues in Vec `Vec::extend` and `Vec::truncate` invalidated references into the vector even without reallocation, because they (implicitly) created a mutable reference covering the *entire* initialized part of the vector. Fixes rust-lang#70301 I verified the fix by adding some new tests to Miri: rust-lang/miri#1253
Fix some aliasing issues in Vec `Vec::extend` and `Vec::truncate` invalidated references into the vector even without reallocation, because they (implicitly) created a mutable reference covering the *entire* initialized part of the vector. Fixes rust-lang#70301 I verified the fix by adding some new tests here that I ran in Miri.
test Vec::extend Currently fails, until rust-lang/rust#70301 gets fixed.
To my knowledge, the following code is intended to be legal:
However, Miri currently flags this as UB.
I believe this is #60847, but for
String
. Discovered while writing this post.cc @RalfJung