Skip to content

Lint transmute where source type is unspecified. #715

Closed
@ticki

Description

@ticki

This is an easy way to create UB. For example:

unsafe {
    let bytes = [0u8; 4];
    let num = *mem::transmute<_, &u32>(bytes);
}

The error (resulting in segfault) might be a little hard to spot. The problem is that the array is transmuted into a null pointer. The transmute argument should be &bytes instead.

By forcing the programmer to add type annotations, he or she is not as likely to commit this mistake (since it will be catched during type checking):

unsafe {
    let bytes = [0; 4];
    let num = *mem::transmute<&[u8; 4], &u32>(&bytes);
}

Since this is a major source of bugs resulting in UB, I propose adding a lint being deny by default, which forces the programmer to annotate the type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-correctnessLint: Belongs in the correctness lint groupT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions