Skip to content

core::ptr::copy unsafe precondition message is for core::ptr::copy_nonoverlapping instead #126400

Closed
@firefighterduck

Description

@firefighterduck

Code

fn main() {
    let mut x = Box::new(42u32);
    let y = x.as_mut() as *mut u32;
    let z = core::ptr::null();

    unsafe {
        core::ptr::copy(z, y, 1);
    }

    println!("Hello, world!");
}

Current output

thread 'main' panicked at library/core/src/panicking.rs:156:5:
unsafe precondition(s) violated: ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null and the specified memory ranges do not overlap
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
Aborted (core dumped)

Desired output

thread 'main' panicked at library/core/src/panicking.rs:156:5:
unsafe precondition(s) violated: ptr::copy requires that both pointer arguments are aligned and non-null
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
Aborted (core dumped)

Rationale and extra context

"ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
and the specified memory ranges do not overlap",

The assert_unsafe_precondition message for core::ptr::copy is the one for core::ptr::copy_nonoverlapping. The precondition itself seems correct, but the message is incorrect in that it not only names the wrong function but also the wrong requirements (i.e., the specified memory ranges do not overlap which copy explicitly allows).

Other cases

No response

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions