Skip to content

Instantly share code, notes, and snippets.

@tomaka
Created February 15, 2016 08:36
Show Gist options
  • Select an option

  • Save tomaka/b579e276b751c42dc3df to your computer and use it in GitHub Desktop.

Select an option

Save tomaka/b579e276b751c42dc3df to your computer and use it in GitHub Desktop.
extern crate libc;
use std::os::raw::c_char;
use std::mem;
use std::slice;
pub struct CStr {
inner: [c_char]
}
impl CStr {
#[inline(never)]
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
let len = libc::strlen(ptr);
mem::transmute(slice::from_raw_parts(ptr, len as usize + 1))
}
}
#[inline(never)]
pub fn hello() -> *const u8 {
unsafe {
let f = CStr::from_ptr(0xdeadbeefusize as *const _);
let v: (*const u8, usize) = mem::transmute(f);
v.0
}
}
pub fn main() {
println!("{:?}", hello());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment