immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
3.81k stars 220 forks source link

analyze: error on void* cast in realloc #988

Open spernsteiner opened 1 year ago

spernsteiner commented 1 year ago

Realloc is supposed to get special handling to prevent the cast in realloc(p as *mut c_void, n) from causing an error. However, this doesn't seem to work reliably.

Example code (add this to tests/filecheck/alloc.rs):

pub unsafe fn realloc2(p: *mut u8, n: usize) {
    let q = realloc(p as *mut libc::c_void, (n + 1) as libc::c_ulong);
}

This produces the error: [ERROR @ c2rust-analyze/src/dataflow/type_check.rs:147 @ c2rust_analyze::dataflow::type_check]: TODO: unsupported ptr-to-ptr cast between pointee types not yet supported as safely transmutable: `*mut u8 as *mut libc::c_void`

Changing n + 1 to n produces a different error:

thread 'rustc' panicked at 'assertion failed: `(left == right)`
  left: `*mut libc::c_void`,
 right: `*mut u8`', c2rust-analyze/src/dataflow/type_check.rs:280:9