Skip to content

Commit

Permalink
Uninitialized boxes: check for zero-size allocation based on Layout::…
Browse files Browse the repository at this point in the history
…size
  • Loading branch information
SimonSapin committed Oct 16, 2019
1 parent 23d3ff1 commit ca1cfda
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ impl<T> Box<T> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
if mem::size_of::<T>() == 0 {
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
if layout.size() == 0 {
return Box(NonNull::dangling().into())
}
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
let ptr = unsafe {
Global.alloc(layout)
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))
Expand Down Expand Up @@ -184,10 +184,10 @@ impl<T> Box<[T]> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
let ptr = if mem::size_of::<T>() == 0 || len == 0 {
let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
let ptr = if layout.size() == 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))
Expand Down

0 comments on commit ca1cfda

Please sign in to comment.