1
Fork 0

Auto merge of #62610 - Stargateur:fix-miri-error-cstring-into_inner, r=RalfJung

Fix miri error in into_inner() of CString

Fix #62553

I choice to not transmute because I think it's more unsafe and in case the structure change this code should always work.

r? @RalfJung
This commit is contained in:
bors 2019-07-14 13:52:40 +00:00
commit 85a360e0ea

View file

@ -599,11 +599,12 @@ impl CString {
///
/// [`Drop`]: ../ops/trait.Drop.html
fn into_inner(self) -> Box<[u8]> {
unsafe {
let result = ptr::read(&self.inner);
mem::forget(self);
result
}
// Rationale: `mem::forget(self)` invalidates the previous call to `ptr::read(&self.inner)`
// so we use `ManuallyDrop` to ensure `self` is not dropped.
// Then we can return the box directly without invalidating it.
// See https://github.com/rust-lang/rust/issues/62553.
let this = mem::ManuallyDrop::new(self);
unsafe { ptr::read(&this.inner) }
}
}