Skip to content

Instantly share code, notes, and snippets.

@yupferris
Created September 27, 2015 14:33
Show Gist options
  • Save yupferris/8d1de1148c824afb96db to your computer and use it in GitHub Desktop.
Save yupferris/8d1de1148c824afb96db to your computer and use it in GitHub Desktop.
Rust transmute/ownership drop test using free
extern crate libc;
use std::mem;
struct Val {
value: i32
}
impl Val {
fn new(value: i32) -> Val {
println!("Created {}", value);
Val { value: value }
}
}
impl Drop for Val {
fn drop(&mut self) {
println!("Destroyed {}", self.value);
}
}
fn main() {
let val = Box::new(Val::new(32));
let ptr: *mut Val = unsafe { mem::transmute(val) };
unsafe { libc::free(ptr as *mut libc::c_void); }
}
~/dev/projects/droptest $ cargo run
Compiling droptest v0.1.0 (file:///Users/yupferris/dev/projects/droptest)
Running `target/debug/droptest`
Created 32
droptest(726,0x7fff7ccb3300) malloc: *** error for object 0x10c825018: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
An unknown error occurred
To learn more, run the command again with --verbose.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment