Skip to content

Commit 4c4bd34

Browse files
committed
Fix miri error in into_inner() of CString
1 parent e31911e commit 4c4bd34

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)