Don't pass 0-length mallocs to system allocator

Part of the unsafe contract!
This commit is contained in:
Alex Crichton 2018-10-28 08:51:47 -07:00
parent 1fa407d2f9
commit 78f425744f

View File

@ -827,10 +827,14 @@ pub mod __rt {
let align = mem::align_of::<usize>(); let align = mem::align_of::<usize>();
if let Ok(layout) = Layout::from_size_align(size, align) { if let Ok(layout) = Layout::from_size_align(size, align) {
unsafe { unsafe {
if layout.size() > 0 {
let ptr = alloc(layout); let ptr = alloc(layout);
if !ptr.is_null() { if !ptr.is_null() {
return ptr return ptr
} }
} else {
return align as *mut u8
}
} }
} }