Make out param stack local, fix for zero sized types

Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
pull/259/head
Daniel Franklin 4 years ago committed by GitHub
parent e542c32c45
commit 75253ec543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -93,10 +93,11 @@ struct Carton<T>(NonNull<T>);
impl<T> Carton<T> {
pub fn new(value: T) -> Self {
// Allocate enough memory on the heap to store one T.
let memptr = &mut null_mut() as *mut *mut T;
assert_ne!(size_of::<T>(), 0, "Zero-sized types are out of the scope of this example);
let memptr: *mut c_void = ptr::null_mut();
unsafe {
let ret = libc::posix_memalign(
memptr as *mut *mut c_void,
&mut memptr,
align_of::<T>(),
size_of::<T>()
);
@ -107,7 +108,7 @@ impl<T> Carton<T> {
let mut ptr = unsafe {
// Safety: memptr is dereferenceable because we created it from a
// reference and have exclusive access.
NonNull::new(*memptr)
NonNull::new(memptr.cast::<T>())
.expect("Guaranteed non-null if posix_memalign returns 0")
};

Loading…
Cancel
Save