String::push_str invalidates interior references even when it does not reallocate · Issue #70301 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@matklad

Description

@matklad

To my knowledge, the following code is intended to be legal:

fn main() { let mut buf = String::with_capacity(11); buf.push_str("hello"); let hello: &str = unsafe { &*(buf.as_str() as *const _) }; // laundering the lifetime -- we take care that buf does not reallocate, so that's okay. buf.push_str(" world"); println!("{}", hello); }

However, Miri currently flags this as UB.

I believe this is #60847, but for String. Discovered while writing this post.

cc @RalfJung