Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop emitting code that triggers the mutable_transmutes lint #1154

Open
bjorn3 opened this issue Oct 30, 2024 · 0 comments
Open

Stop emitting code that triggers the mutable_transmutes lint #1154

bjorn3 opened this issue Oct 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@bjorn3
Copy link

bjorn3 commented Oct 30, 2024

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.

@kkysen kkysen added the bug Something isn't working label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants