Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with field retagging in scopeguard #359

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/miri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rustup toolchain install nightly --component miri
rustup override set nightly
cargo miri setup

cargo miri test
MIRIFLAGS='-Zmiri-retag-fields' cargo miri test
14 changes: 6 additions & 8 deletions src/scopeguard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Extracted from the scopeguard crate
use core::{
mem,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
ptr,
};
Expand Down Expand Up @@ -28,15 +28,13 @@ where
#[inline]
pub fn into_inner(guard: Self) -> T {
// Cannot move out of Drop-implementing types, so
// ptr::read the value and forget the guard.
// ptr::read the value out of a ManuallyDrop<Self>
// Don't use mem::forget as that might invalidate value
let guard = ManuallyDrop::new(guard);
unsafe {
let value = ptr::read(&guard.value);
// read the closure so that it is dropped, and assign it to a local
// variable to ensure that it is only dropped after the guard has
// been forgotten. (In case the Drop impl of the closure, or that
// of any consumed captured variable, panics).
let _dropfn = ptr::read(&guard.dropfn);
mem::forget(guard);
// read the closure so that it is dropped
let _ = ptr::read(&guard.dropfn);
value
}
}
Expand Down