From 75253ec543b38cfab3bfb9d8b49bcb5971c0c7e1 Mon Sep 17 00:00:00 2001 From: Daniel Franklin Date: Tue, 23 Mar 2021 21:14:34 +0000 Subject: [PATCH] Make out param stack local, fix for zero sized types Co-authored-by: Daniel Henry-Mantilla --- src/send-and-sync.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/send-and-sync.md b/src/send-and-sync.md index fd1bb94..33860ef 100644 --- a/src/send-and-sync.md +++ b/src/send-and-sync.md @@ -93,10 +93,11 @@ struct Carton(NonNull); impl Carton { 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::(), 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::(), size_of::() ); @@ -107,7 +108,7 @@ impl Carton { 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::()) .expect("Guaranteed non-null if posix_memalign returns 0") };