Add debug asserts to UntypedArenaPtr::build_with, ::get_tag and raw_ptr_as_cell!

These two functions are pretty unsafe, but having these assertions makes
it easier to catch UB in testing.
This commit is contained in:
Emilie Burgun
2025-01-31 15:12:54 +01:00
parent c548b14f50
commit 079a69396a
2 changed files with 3 additions and 0 deletions

View File

@@ -175,6 +175,7 @@ macro_rules! raw_ptr_as_cell {
// TODO use <*{const,mut} _>::addr instead of as when the strict_provenance feature is stable rust-lang/rust#95228 // TODO use <*{const,mut} _>::addr instead of as when the strict_provenance feature is stable rust-lang/rust#95228
// we might need <*{const,mut} _>::expose_provenance for strict provenance, dependening on how we recreate a pointer later // we might need <*{const,mut} _>::expose_provenance for strict provenance, dependening on how we recreate a pointer later
let ptr : *const _ = $ptr; let ptr : *const _ = $ptr;
debug_assert!(!$ptr.is_null());
HeapCellValue::from_ptr_addr(ptr as usize) HeapCellValue::from_ptr_addr(ptr as usize)
}}; }};
} }

View File

@@ -656,6 +656,7 @@ pub struct UntypedArenaPtr {
impl UntypedArenaPtr { impl UntypedArenaPtr {
#[inline(always)] #[inline(always)]
pub fn build_with(ptr: usize) -> Self { pub fn build_with(ptr: usize) -> Self {
debug_assert!(ptr != 0);
UntypedArenaPtr::new().with_ptr(ptr as u64) UntypedArenaPtr::new().with_ptr(ptr as u64)
} }
} }
@@ -698,6 +699,7 @@ impl UntypedArenaPtr {
#[inline] #[inline]
pub fn get_tag(self) -> ArenaHeaderTag { pub fn get_tag(self) -> ArenaHeaderTag {
unsafe { unsafe {
debug_assert!(!self.get_ptr().is_null());
let header = *(self.get_ptr() as *const ArenaHeader); let header = *(self.get_ptr() as *const ArenaHeader);
header.get_tag() header.get_tag()
} }