Skip to content

Commit

Permalink
Replace mem::forget with ManuallyDrop, fixing potential UB
Browse files Browse the repository at this point in the history
As shown in rust-lang/miri#1508,
it was possible to "find UB" in `replace_with` with Miri.
  • Loading branch information
steffahn committed Aug 12, 2020
1 parent c3730ae commit 4039fc0
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct OnDrop<F: FnOnce()>(mem::ManuallyDrop<F>);
impl<F: FnOnce()> Drop for OnDrop<F> {
#[inline(always)]
fn drop(&mut self) {
(unsafe { ptr::read(&*self.0) })();
(unsafe { mem::ManuallyDrop::take(&mut self.0) })();
}
}

Expand All @@ -103,8 +103,8 @@ impl<F: FnOnce()> Drop for OnDrop<F> {
pub fn on_unwind<F: FnOnce() -> T, T, P: FnOnce()>(f: F, p: P) -> T {
let x = OnDrop(mem::ManuallyDrop::new(p));
let t = f();
let _ = unsafe { ptr::read(&*x.0) };
mem::forget(x);
let mut x = mem::ManuallyDrop::new(x);
unsafe { mem::ManuallyDrop::drop(&mut x.0) };
t
}

Expand Down

0 comments on commit 4039fc0

Please sign in to comment.