guard against zero sized types with an error

We could update this to just not allocate in the future but I don't believe that the docs give us assurances in that area.
This commit is contained in:
Lokathor 2019-09-20 13:05:09 -06:00 committed by GitHub
parent 6fea4bfa58
commit dad138dba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,6 +42,9 @@ pub fn try_cast_box<A: Pod, B: Pod>(input: Box<A>) -> Result<Box<B>, (PodCastErr
/// large boxes without fear of a stack overflow.
#[inline]
pub fn try_zeroed_box<T: Zeroable>() -> Result<Box<T>, ()> {
if size_of::<T>() == 0 {
return Err(());
}
let layout = Layout::from_size_align(size_of::<T>(), align_of::<T>()).unwrap();
let ptr = unsafe { alloc_zeroed(layout) };
if ptr.is_null() {