Skip to content

Invalid equality check result for usizes obtained from function pointers #73722

Closed
@Riateche

Description

@Riateche

Code:

fn main() {
    fn x() {}
    fn y() {}

    let a = x as usize;
    let b = y as usize;
    
    assert_eq!(a, b);
}

Output in release mode:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `94740677858160`,
 right: `94740677858160`', src/main.rs:8:5

Playground

There is a similar issue about comparing function pointers, but incorrect comparison of usize may be more dangerous. For example, if unsafe code checks the equality between usize to uphold its invariant, passing values obtained this way may break the invariant, even though the unsafe code itself didn't work with function pointers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityT-compilerRelevant to the compiler 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