Skip to content

Audit uses of addr_of! for read-only-ness #1607

Description

@joshlf

Confirm that our uses of addr_of! are sound in light of rust-lang/rust#129653. As of this writing, it appears that there are two uses:

One is Unalign::get_ptr:

zerocopy/src/wrappers.rs

Lines 233 to 235 in b8c3178

pub const fn get_ptr(&self) -> *const T {
ptr::addr_of!(self.0)
}

One is in trailing_field_offset!:

zerocopy/src/macro_util.rs

Lines 187 to 189 in b8c3178

let field = unsafe {
$crate::macro_util::core_reexport::ptr::addr_of!((*ptr).$trailing_field_name)
};

The Unalign::get_ptr one may be problematic if the user uses the returned *const T to perform mutation. We may need to clarify in the safety docs on that method that the returned pointer is read-only.

Activity

  1. added a commit that references this issue on Sep 7, 2024
    17b34cc
  2. added a commit that references this issue on Sep 7, 2024
    403a890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocking-next-releaseThis issue should be resolved before we release on crates.io

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions