We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e31911e commit 4c4bd34Copy full SHA for 4c4bd34
src/libstd/ffi/c_str.rs
@@ -599,11 +599,12 @@ impl CString {
599
///
600
/// [`Drop`]: ../ops/trait.Drop.html
601
fn into_inner(self) -> Box<[u8]> {
602
- unsafe {
603
- let result = ptr::read(&self.inner);
604
- mem::forget(self);
605
- result
606
- }
+ // 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);
607
+ unsafe { ptr::read(&this.inner) }
608
}
609
610
0 commit comments