Skip to content

Imprecise error message when doing inbounds pointer arithmetic on pointer to freed allocation #2859

Closed
@DoubleHyphen

Description

@DoubleHyphen

This is probably rather niche, but it seems like a low-hanging fruit.
Here is a code example:

    let x: *const u8 = {
        let a = 0u8;
        &a
    };
    unsafe 
    {
        let x = x.add(0);
    }

Error message

error: Undefined Behavior: pointer to alloc1494 was dereferenced after this allocation got freed

Except… nothing was dereferenced. The pointer's value didn't even change.

From what I gathered by reading the documentation, .add creates immediate UB if it outputs a pointer whose dereferencing would be illegal, which is exactly the case here; indeed, although the value doesn't change, it's UB according to the documentation. But it's not exactly correct to claim that it was dereferenced when it wasn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions