Skip to content

Commit ca1cfda

Browse files
committed
Uninitialized boxes: check for zero-size allocation based on Layout::size
1 parent 23d3ff1 commit ca1cfda

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/liballoc/boxed.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ impl<T> Box<T> {
141141
/// ```
142142
#[unstable(feature = "new_uninit", issue = "63291")]
143143
pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
144-
if mem::size_of::<T>() == 0 {
144+
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
145+
if layout.size() == 0 {
145146
return Box(NonNull::dangling().into())
146147
}
147-
let layout = alloc::Layout::new::<mem::MaybeUninit<T>>();
148148
let ptr = unsafe {
149149
Global.alloc(layout)
150150
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))
@@ -184,10 +184,10 @@ impl<T> Box<[T]> {
184184
/// ```
185185
#[unstable(feature = "new_uninit", issue = "63291")]
186186
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
187-
let ptr = if mem::size_of::<T>() == 0 || len == 0 {
187+
let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
188+
let ptr = if layout.size() == 0 {
188189
NonNull::dangling()
189190
} else {
190-
let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
191191
unsafe {
192192
Global.alloc(layout)
193193
.unwrap_or_else(|_| alloc::handle_alloc_error(layout))

0 commit comments

Comments
 (0)