Auto merge of #164034 - maxdexh:global-alloc-dangling, r=clarfonthey

Don't rely on `Global` handing out dangling pointers



This code is incorrect and will leak memory once the global allocator supports zero-sized allocations, since `Box` doesn't deallocate zero-sized layouts
This commit is contained in:
bors
2026-10-10 06:48:47 +00:00

View File

@@ -248,6 +248,9 @@ pub struct Box<
#[cfg(not(no_global_oom_handling))]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
const fn box_new_uninit(layout: Layout) -> *mut u8 {
if layout.size() == 0 {
return layout.dangling_ptr().as_ptr();
}
match Global.allocate(layout) {
Ok(ptr) => ptr.as_mut_ptr(),
Err(_) => handle_alloc_error(layout),