diff options
| author | Antoine PLASKOWSKI <antoine.plaskowski@epitech.eu> | 2019-07-12 09:30:58 +0200 |
|---|---|---|
| committer | Antoine PLASKOWSKI <antoine.plaskowski@epitech.eu> | 2019-07-13 11:09:46 +0200 |
| commit | 4c4bd3406705066223e9dddb909de7eddbeac861 (patch) | |
| tree | 9e9963c490330bbe0f95904f67cb2523825fdc43 /src/libstd | |
| parent | e31911ef8f1f46b7fab6e4f350679822ac7d7f6a (diff) | |
| download | rust-4c4bd3406705066223e9dddb909de7eddbeac861.tar.gz rust-4c4bd3406705066223e9dddb909de7eddbeac861.zip | |
Fix miri error in into_inner() of CString
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 5c6c43017cf..585731fb30e 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -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) } } } |
