test pointer is safely writeable

avec
Avril 4 years ago
parent 02bcfeb3fe
commit ab99d0927b
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -9,19 +9,32 @@ mod tests {
{ {
use std::ffi::c_void; use std::ffi::c_void;
let mut size: usize = 100; let size: usize = 100;
let output = {
let mut size: usize = size;
extern "C" fn callback(ptr: *mut c_void, data: *mut c_void) extern "C" fn callback(ptr: *mut c_void, data: *mut c_void)
{ {
let size = unsafe {&mut *(data as *mut usize)}; let size = unsafe {&mut *(data as *mut usize)};
println!("From callback! Size is {}", *size); let slice = unsafe {
std::ptr::write_bytes(ptr, 0, *size);
std::slice::from_raw_parts_mut(ptr as *mut u8, *size)
};
println!("From callback! Size is {}", slice.len());
*size = 0; for (i, x) in (0..).zip(slice.iter_mut())
{
*x = i as u8;
}
*size = slice.iter().map(|&x| x as usize).sum::<usize>();
} }
unsafe { unsafe {
super::ffi::alloca_trampoline(size, callback, &mut size as *mut usize as *mut _); super::ffi::alloca_trampoline(size, callback, &mut size as *mut usize as *mut _);
} }
size
};
assert_eq!(size, 0); assert_eq!(output, (0..size).sum::<usize>());
} }
} }

Loading…
Cancel
Save