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;
extern "C" fn callback(ptr: *mut c_void, data: *mut c_void) let output = {
{ let mut size: usize = size;
let size = unsafe {&mut *(data as *mut usize)}; extern "C" fn callback(ptr: *mut c_void, data: *mut c_void)
println!("From callback! Size is {}", *size); {
let size = unsafe {&mut *(data as *mut usize)};
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;
}
unsafe { *size = slice.iter().map(|&x| x as usize).sum::<usize>();
super::ffi::alloca_trampoline(size, callback, &mut size as *mut usize as *mut _); }
}
assert_eq!(size, 0); unsafe {
super::ffi::alloca_trampoline(size, callback, &mut size as *mut usize as *mut _);
}
size
};
assert_eq!(output, (0..size).sum::<usize>());
} }
} }

Loading…
Cancel
Save