Skip to content

Commit 2aa29bb

Browse files
authored
Merge pull request #4712 from TheBlueMatt/2026-04-overalloc
Avoid over-allocating when reading corrupted lengths for `HashMap`s
2 parents 428746a + 5b4626f commit 2aa29bb

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

lightning/src/util/ser.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,9 @@ macro_rules! impl_for_map {
960960
#[inline]
961961
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
962962
let len: CollectionLength = Readable::read(r)?;
963-
let mut ret = $constr(len.0 as usize);
963+
let entry_size = ::core::mem::size_of::<K>() + ::core::mem::size_of::<V>();
964+
let max_alloc = MAX_BUF_SIZE / (entry_size + 1);
965+
let mut ret = $constr(cmp::min(len.0 as usize, max_alloc));
964966
for _ in 0..len.0 {
965967
let k = K::read(r)?;
966968
let v_opt = V::read(r)?;

0 commit comments

Comments
 (0)