Skip to content

Commit

Permalink
Fix miri error in into_inner() of CString
Browse files Browse the repository at this point in the history
  • Loading branch information
Stargateur committed Jul 13, 2019
1 parent e31911e commit 4c4bd34
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}

Expand Down

0 comments on commit 4c4bd34

Please sign in to comment.