Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing safety in {ptr,NonNull}::as_mut #138919

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

DiuDiu777
Copy link
Contributor

@DiuDiu777 DiuDiu777 commented Mar 25, 2025

In this PR:

  1. The initialization precondition for ptr::{ as_mut, as_ref, as_mut_unchecked, as_ref_unchecked } and NonNull::{ as_mut, as_ref } have been moved under the safety contracts.
  2. Specified the potential aliasing risks of APIs that return a reference, including ptr::{ as_mut, as_ref, as_mut_unchecked, as_ref_unchecked, as_uninit_mut, as_uninit_ref } and NonNull::{ as_mut, as_ref, as_uninit_mut, as_uninit_ref }. The following code passes the compiler's exclusivity checks, but Miri reports undefined behavior.
fn main() {
    use std::ptr::NonNull;
    let mut x = 0u32;
    let mut ptr = NonNull::new(&mut x).expect("null pointer");
    let x_ref = unsafe { ptr.as_mut() };
    unsafe { 
        *ptr.as_ptr() += 2;  
    }
    *x_ref += 2;
    println!("Final value: {}", x_ref);
}

@rustbot
Copy link
Collaborator

rustbot commented Mar 25, 2025

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants