Skip to content

new lint: pointer created from transmuting an int into a pointer is not able to be dereferenced #13140

Open
@timrobertsdev

Description

@timrobertsdev

What it does

Points out potential UB when transmuting an integer into a pointer.

As of Rust 1.78, LLVM is informed that this is UB. In <= 1.77 this works, and the change caught me off guard, as an int->ptr conversion happens a lot when dealing with win32.

Advantage

The original code will cause UB if the pointer is dereferenced. The recommended code works as intended.

Drawbacks

Possible false positives, though I'm not sure of the use case.

Example

// win32 tends to hand out isize and wants you to use them as pointers
let l_param: isize = ...; 
let evt: *mut MSLLHOOKSTRUCT = std::mem::transmute(l_param);

Could be written as:

let l_param: isize = ...;
let evt = l_param as *mut MSLLHOOKSTRUCT;

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions