handle oob heap index calculation

This commit is contained in:
Skgland
2025-12-07 21:45:13 +01:00
committed by Bennet Bleßmann
parent d63b0a192c
commit 11901b5fde
2 changed files with 14 additions and 8 deletions

View File

@@ -623,7 +623,7 @@ impl Heap {
pub fn reserve(&mut self, num_cells: usize) -> Result<HeapWriter<'_>, AllocError> {
let section;
let len = heap_index!(num_cells);
let len = heap_index_checked!(num_cells).ok_or(AllocError)?;
loop {
unsafe {

View File

@@ -470,16 +470,22 @@ macro_rules! resource_error_call_result {
};
}
macro_rules! heap_index_checked {
($idx:expr) => {
std::mem::size_of::<HeapCellValue>().checked_mul($idx)
};
}
pub(crate) use heap_index_checked;
macro_rules! heap_index {
($idx:expr) => {{
let idx = $idx;
std::mem::size_of::<HeapCellValue>()
.checked_mul(idx)
.expect(&format!(
"overflow while calculating heap index {idx} * {} > {}",
std::mem::size_of::<HeapCellValue>(),
usize::MAX,
))
$crate::macros::heap_index_checked!(idx).expect(&format!(
"overflow while calculating heap index {idx} * {} > {}",
std::mem::size_of::<HeapCellValue>(),
usize::MAX,
))
}};
}