Skip to content

Commit 403a890

Browse files
authored
Tighten soundness of addr_of! uses (#1617)
Closes #1607
1 parent f7d572a commit 403a890

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/macro_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ macro_rules! trailing_field_offset {
174174
// allocation addressed by `ALIGNED_64K_ALLOCATION` is guaranteed to
175175
// be aligned to `_64K`, so `ptr` is guaranteed to satisfy `$ty`'s
176176
// alignment.
177+
// - As required by `addr_of!`, we do not write through `field`.
177178
//
178179
// Note that, as of [2], this requirement is technically unnecessary
179180
// for Rust versions >= 1.75.0, but no harm in guaranteeing it anyway

src/wrappers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,13 @@ impl<T> Unalign<T> {
228228
/// documented as being sound to use with an unaligned pointer, such as
229229
/// [`read_unaligned`].
230230
///
231+
/// Even if the caller is permitted to mutate `self` (e.g. they have
232+
/// ownership or a mutable borrow), it is not guaranteed to be sound to
233+
/// write through the returned pointer. If writing is required, prefer
234+
/// [`get_mut_ptr`] instead.
235+
///
231236
/// [`read_unaligned`]: core::ptr::read_unaligned
237+
/// [`get_mut_ptr`]: Unalign::get_mut_ptr
232238
#[inline(always)]
233239
pub const fn get_ptr(&self) -> *const T {
234240
ptr::addr_of!(self.0)

0 commit comments

Comments
 (0)