Skip to content

Commit 85a360e

Browse files
committed
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
2 parents 7d41ebf + 4c4bd34 commit 85a360e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Diff for: src/libstd/ffi/c_str.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,12 @@ impl CString {
599599
///
600600
/// [`Drop`]: ../ops/trait.Drop.html
601601
fn into_inner(self) -> Box<[u8]> {
602-
unsafe {
603-
let result = ptr::read(&self.inner);
604-
mem::forget(self);
605-
result
606-
}
602+
// Rationale: `mem::forget(self)` invalidates the previous call to `ptr::read(&self.inner)`
603+
// so we use `ManuallyDrop` to ensure `self` is not dropped.
604+
// Then we can return the box directly without invalidating it.
605+
// See https://github.com/rust-lang/rust/issues/62553.
606+
let this = mem::ManuallyDrop::new(self);
607+
unsafe { ptr::read(&this.inner) }
607608
}
608609
}
609610

0 commit comments

Comments
 (0)