From 11901b5fdefb6b7ef86eca0fba00d79bae8cbad4 Mon Sep 17 00:00:00 2001 From: Skgland Date: Sun, 7 Dec 2025 21:45:13 +0100 Subject: [PATCH] handle oob heap index calculation --- src/machine/heap.rs | 2 +- src/macros.rs | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/machine/heap.rs b/src/machine/heap.rs index d9f1aa88..3d614ebb 100644 --- a/src/machine/heap.rs +++ b/src/machine/heap.rs @@ -623,7 +623,7 @@ impl Heap { pub fn reserve(&mut self, num_cells: usize) -> Result, AllocError> { let section; - let len = heap_index!(num_cells); + let len = heap_index_checked!(num_cells).ok_or(AllocError)?; loop { unsafe { diff --git a/src/macros.rs b/src/macros.rs index 4ac590eb..a5e16b8b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -470,16 +470,22 @@ macro_rules! resource_error_call_result { }; } +macro_rules! heap_index_checked { + ($idx:expr) => { + std::mem::size_of::().checked_mul($idx) + }; +} + +pub(crate) use heap_index_checked; + macro_rules! heap_index { ($idx:expr) => {{ let idx = $idx; - std::mem::size_of::() - .checked_mul(idx) - .expect(&format!( - "overflow while calculating heap index {idx} * {} > {}", - std::mem::size_of::(), - usize::MAX, - )) + $crate::macros::heap_index_checked!(idx).expect(&format!( + "overflow while calculating heap index {idx} * {} > {}", + std::mem::size_of::(), + usize::MAX, + )) }}; } -- 2.54.0