Skip to content

Commit 82a22ee

Browse files
authored
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 3a80127 commit 82a22ee

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
@@ -2,7 +2,7 @@ use crate::span::Span;
22
use core::{
33
future::Future,
44
marker::Sized,
5-
mem::{self, ManuallyDrop},
5+
mem::ManuallyDrop,
66
pin::Pin,
77
task::{Context, Poll},
88
};
@@ -392,12 +392,11 @@ impl<T> Instrumented<T> {
392392
///
393393
/// Note that this drops the span.
394394
pub fn into_inner(self) -> T {
395-
// To manually destructure `Instrumented` without `Drop`, we save
396-
// pointers to the fields and use `mem::forget` to leave those pointers
397-
// valid.
398-
let span: *const Span = &self.span;
399-
let inner: *const ManuallyDrop<T> = &self.inner;
400-
mem::forget(self);
395+
// To manually destructure `Instrumented` without `Drop`, we
396+
// move it into a ManuallyDrop and use pointers to its fields
397+
let this = ManuallyDrop::new(self);
398+
let span: *const Span = &this.span;
399+
let inner: *const ManuallyDrop<T> = &this.inner;
401400
// SAFETY: Those pointers are valid for reads, because `Drop` didn't
402401
// run, and properly aligned, because `Instrumented` isn't
403402
// `#[repr(packed)]`.

0 commit comments

Comments
 (0)