Skip to content

Instantly share code, notes, and snippets.

@mitghi
Created August 28, 2021 08:28
Show Gist options
  • Save mitghi/a101d8ea6c84d6041997a893b67bc1db to your computer and use it in GitHub Desktop.
Save mitghi/a101d8ea6c84d6041997a893b67bc1db to your computer and use it in GitHub Desktop.
Bad Rust
struct A {
value: *mut i64,
}
impl A {
fn new() -> A {
A{value: std::ptr::null_mut()}
}
}
fn dostuff(val: &i64, ia: &A) {
unsafe {
let a = val as *const i64 as *mut i64;
let b = ia as *const A as *mut A;
*a = 100;
(*b).value = a;
}
}
fn main() {
let ia = A::new();
let a = 10;
dostuff(&a, &ia);
unsafe {
println!("{}", *(ia.value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment