You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nullable c_import pointers can miscompile when checked against None and unwrapped #30
Nullable C pointers imported through c_import still miscompile in normal control flow when stored in a local, compared against None, and then unwrapped.
Minimal repro:
use c_import("stdlib.h")
fn make_handle -> i64:
let raw_opt = malloc(8)
if raw_opt == None:
return 0
let raw = raw_opt.unwrap()
let _ = realloc(raw, 0)
1
fn main:
assert(make_handle() == 1)
Observed behavior
with run reports:
error: RK_AGGREGATE with unknown dest type fn=make_handle count=0
LLVM verify error: Both operands to ICmp instruction are not of the same type!
%14 = icmp eq ptr %12, i64 %13, !dbg !393
The same pattern in a Result-returning function also emits invalid LLVM:
use c_import("stdlib.h")
pub error E = Bad
fn allocish -> Result[i32, E]:
let raw_opt = malloc(8)
if raw_opt == None:
return Err(.Bad)
let raw = raw_opt.unwrap()
let _ = realloc(raw, 0)
Ok(1)
Expected behavior
A nullable C pointer should behave like a normal Option in local control flow. Comparing it against None and calling .unwrap() after the check should either work or be rejected with a front-end error, not emit invalid LLVM IR.
Repro
cat > /tmp/c_pointer_option_bug.w << 'EOF2'
use c_import("stdlib.h")
fn make_handle -> i64:
let raw_opt = malloc(8)
if raw_opt == None:
return 0
let raw = raw_opt.unwrap()
let _ = realloc(raw, 0)
1
fn main:
assert(make_handle() == 1)
EOF2
with run /tmp/c_pointer_option_bug.w
Activity
Fix record updates and nullable c_import pointers