-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
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
The text was updated successfully, but these errors were encountered: