Skip to content

Incorrect error message for offset_from() on pointers to two different allocations. #4143

@theemathas

Description

@theemathas

I ran the following code with Miri:

fn main() {
    unsafe {
        (&1_u8 as *const u8).offset_from(&2_u8);
    }
}

I expected Miri to sensibly report some UB, but instead I got the following:

error: Undefined Behavior: out-of-bounds `offset_from` origin: expected a pointer to the end of 4 bytes of memory, but got alloc4 which is at the beginning of the allocation
 --> src/main.rs:3:9
  |
3 |         (&1_u8 as *const u8).offset_from(&2_u8);
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from` origin: expected a pointer to the end of 4 bytes of memory, but got alloc4 which is at the beginning of the allocation
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
  = note: BACKTRACE:
  = note: inside `main` at src/main.rs:3:9: 3:48

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

The error message doesn't make any sense. I don't know where Miri got the "4 bytes of memory" expectation from. So I think the error is incorrect.

Reproducible on the playground with rust version 1.86.0-nightly (2025-01-20 f3d1d47fd84dfcf7f513)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticserrors and warnings emitted by miriA-intrinsicsArea: Affects out implementation of Rust intrinsicsC-enhancementCategory: a PR with an enhancement or an issue tracking an accepted enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions