Skip to content

Commit 2f15440

Browse files
committed
Use ManuallyDrop<T> instead of mem::forget in scopeguard
With field retagging in SB, this causes UB because the read out value is invalidated by the move into `mem::forget`. Using `ManuallyDrop<T>` avoids the problem.
1 parent 22c7dbc commit 2f15440

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/scopeguard.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Extracted from the scopeguard crate
22
use core::{
3-
mem,
3+
mem::{ManuallyDrop},
44
ops::{Deref, DerefMut},
55
ptr,
66
};
@@ -28,15 +28,16 @@ where
2828
#[inline]
2929
pub fn into_inner(guard: Self) -> T {
3030
// Cannot move out of Drop-implementing types, so
31-
// ptr::read the value and forget the guard.
31+
// ptr::read the value out of a ManuallyDrop<Self>
32+
// Don't use mem::forget as that might invalidate `value`
33+
let guard = ManuallyDrop::new(guard);
3234
unsafe {
3335
let value = ptr::read(&guard.value);
3436
// read the closure so that it is dropped, and assign it to a local
3537
// variable to ensure that it is only dropped after the guard has
3638
// been forgotten. (In case the Drop impl of the closure, or that
3739
// of any consumed captured variable, panics).
3840
let _dropfn = ptr::read(&guard.dropfn);
39-
mem::forget(guard);
4041
value
4142
}
4243
}

0 commit comments

Comments
 (0)