Skip to content

Commit 20a1762

Browse files
Manishearthhawkw
authored andcommitted
tracing: use ManuallyDrop instead of mem::forget (#2765)
The current code is UB and LLVM could choose to reuse the stack slot causing a UAF. ## Motivation UB is bad. ## Solution Don't do that.
1 parent 4b99457 commit 20a1762

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

tracing/src/instrument.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use core::{
66
future::Future,
77
marker::Sized,
8-
mem::{self, ManuallyDrop},
8+
mem::ManuallyDrop,
99
pin::Pin,
1010
task::{Context, Poll},
1111
};
@@ -359,12 +359,11 @@ impl<T> Instrumented<T> {
359359
///
360360
/// Note that this drops the span.
361361
pub fn into_inner(self) -> T {
362-
// To manually destructure `Instrumented` without `Drop`, we save
363-
// pointers to the fields and use `mem::forget` to leave those pointers
364-
// valid.
365-
let span: *const Span = &self.span;
366-
let inner: *const ManuallyDrop<T> = &self.inner;
367-
mem::forget(self);
362+
// To manually destructure `Instrumented` without `Drop`, we
363+
// move it into a ManuallyDrop and use pointers to its fields
364+
let this = ManuallyDrop::new(self);
365+
let span: *const Span = &this.span;
366+
let inner: *const ManuallyDrop<T> = &this.inner;
368367
// SAFETY: Those pointers are valid for reads, because `Drop` didn't
369368
// run, and properly aligned, because `Instrumented` isn't
370369
// `#[repr(packed)]`.

0 commit comments

Comments
 (0)