From 155aef9d64b3dc5ad38de70289f576e311736672 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 23 Jul 2024 01:12:29 -0700 Subject: [PATCH] std: Unsafe-wrap alloc code held in-common --- std/src/sys/pal/common/alloc.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/std/src/sys/pal/common/alloc.rs b/std/src/sys/pal/common/alloc.rs index 598b6db71f5de..54506c3229675 100644 --- a/std/src/sys/pal/common/alloc.rs +++ b/std/src/sys/pal/common/alloc.rs @@ -1,3 +1,4 @@ +#![forbid(unsafe_op_in_unsafe_fn)] use crate::alloc::{GlobalAlloc, Layout, System}; use crate::cmp; use crate::ptr; @@ -46,14 +47,16 @@ pub unsafe fn realloc_fallback( old_layout: Layout, new_size: usize, ) -> *mut u8 { - // Docs for GlobalAlloc::realloc require this to be valid: - let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align()); + // SAFETY: Docs for GlobalAlloc::realloc require this to be valid + unsafe { + let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align()); - let new_ptr = GlobalAlloc::alloc(alloc, new_layout); - if !new_ptr.is_null() { - let size = cmp::min(old_layout.size(), new_size); - ptr::copy_nonoverlapping(ptr, new_ptr, size); - GlobalAlloc::dealloc(alloc, ptr, old_layout); + let new_ptr = GlobalAlloc::alloc(alloc, new_layout); + if !new_ptr.is_null() { + let size = cmp::min(old_layout.size(), new_size); + ptr::copy_nonoverlapping(ptr, new_ptr, size); + GlobalAlloc::dealloc(alloc, ptr, old_layout); + } + new_ptr } - new_ptr }