Skip to content

Commit bec140b

Browse files
committed
fix(allocator): avoid dereferencing Box when obtaining its Address (#15322)
Use `Box::as_non_null` method (added in #15321) to ensure that creating an `Address` from a `Box`, no intermediate reference to the contents of the `Box` is created. This is important in `Traverse` which has tight aliasing constraints. Possibly `addr_of!` macro we used previously already ensured this, but I'm not 100% sure due to the double deref (`**`). Using `Box::as_non_null` is unambiguously safe.
1 parent 1274291 commit bec140b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

crates/oxc_allocator/src/address.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ptr::{NonNull, addr_of};
1+
use std::ptr::NonNull;
22

33
use crate::Box;
44

@@ -160,7 +160,8 @@ impl<T> GetAddress for Box<'_, T> {
160160
/// so this address acts as a unique identifier for the duration of the arena's existence.
161161
#[inline]
162162
fn address(&self) -> Address {
163-
Address::from_ptr(addr_of!(**self))
163+
let ptr = Box::as_non_null(self).as_ptr().cast_const();
164+
Address::from_ptr(ptr)
164165
}
165166
}
166167

0 commit comments

Comments
 (0)