Skip to content

Stop emitting code that triggers the mutable_transmutes lint #1154

@bjorn3

Description

@bjorn3

Transmuting from a reference to a mutable reference is insta-UB: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8da5074ba78a22ba108e198273c547ba

error: Undefined Behavior: writing to alloc1 which is read-only
 --> src/main.rs:5:17
  |
5 |         let _ = core::mem::transmute::<&[u8; 10], &mut [libc::c_char; 10]>(b"\0\0\0\0\0\0\0\0\0\0");
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ writing to alloc1 which is read-only
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
  = note: BACKTRACE:
  = note: inside `main` at src/main.rs:5:17: 5:100

C2rust currently does such transmutes for code like char mode2[10] = ""; where it produces let mut mode2: [libc::c_char; 10] = *core::mem::transmute::<&[u8; 10], &mut [libc::c_char; 10]>(b"\0\0\0\0\0\0\0\0\0\0"); even though let mut mode2: [libc::c_char; 10] = [0; 10]; works just fine.

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