about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-07-14 13:52:40 +0000
committerbors <bors@rust-lang.org>2019-07-14 13:52:40 +0000
commit85a360e0ea2f1629b8851e7c9b2903bbdbab42bf (patch)
treee6492485d8bb23d0856f0dfb1e9e046e368f3d46 /src
parent7d41ebf768faca490addc7c616b3a9274621f0e9 (diff)
parent4c4bd3406705066223e9dddb909de7eddbeac861 (diff)
downloadrust-85a360e0ea2f1629b8851e7c9b2903bbdbab42bf.tar.gz
rust-85a360e0ea2f1629b8851e7c9b2903bbdbab42bf.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/libstd/ffi/c_str.rs11
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) }
     }
 }