Skip to content

Commit

Permalink
Fix zero-size uninitialized boxes
Browse files Browse the repository at this point in the history
Requesting a zero-size allocation is not allowed,
return a dangling pointer instead.

CC #63291 (comment)
  • Loading branch information
SimonSapin committed Oct 6, 2019
1 parent 421bd77 commit 23d3ff1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/liballoc/boxed.rs
Expand Up @@ -141,6 +141,9 @@ impl<T> Box<T> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
if mem::size_of::<T>() == 0 {
return Box(NonNull::dangling().into())
}
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
let ptr = unsafe {
Global.alloc(layout)
Expand Down Expand Up @@ -181,10 +184,17 @@ impl<T> Box<[T]> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
let ptr = unsafe { alloc::alloc(layout) };
let unique = Unique::new(ptr).unwrap_or_else(|| alloc::handle_alloc_error(layout));
let slice = unsafe { slice::from_raw_parts_mut(unique.cast().as_ptr(), len) };
let ptr = if mem::size_of::<T>() == 0 || len == 0 {
NonNull::dangling()
} else {
let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
unsafe {
Global.alloc(layout)
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))
.cast()
}
};
let slice = unsafe { slice::from_raw_parts_mut(ptr.as_ptr(), len) };
Box(Unique::from(slice))
}
}
Expand Down

0 comments on commit 23d3ff1

Please sign in to comment.