Skip to content

Miscompilation of unsigned division #967

Description

@Amanieu
use std::hint::black_box;
use std::mem::transmute;

fn main() {
    let pointer = black_box(usize::MAX) as *const ();
    let value = unsafe { transmute::<*const (), usize>(pointer) };

    assert_eq!(value / black_box(2), usize::MAX / 2);
    assert_eq!(value % black_box(2), usize::MAX % 2);
}
assertion `left == right` failed
  left: 0
 right: 9223372036854775807

It seems that a pointer transmuted to usize is somehow being treated as a signed number, which causes signed division to be used instead of unsigned division.

Originally found in rust-lang/rust#157943 and reduced using AI.

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions