Skip to content

Commit d71acb5

Browse files
authored
fix(EOF): Overflow on num_sections (#1656)
* fix(EOF): Overflow on num_sections * fix test * fmt/clippy
1 parent afb8083 commit d71acb5

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

crates/primitives/src/bytecode/eof/header.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ fn consume_header_section_size(input: &[u8]) -> Result<(&[u8], Vec<u16>, usize),
3939
if num_sections == 0 {
4040
return Err(EofDecodeError::NonSizes);
4141
}
42-
let byte_size = (num_sections * 2) as usize;
42+
let num_sections = num_sections as usize;
43+
let byte_size = num_sections * 2;
4344
if input.len() < byte_size {
4445
return Err(EofDecodeError::ShortInputForSizes);
4546
}
46-
let mut sizes = Vec::with_capacity(num_sections as usize);
47+
let mut sizes = Vec::with_capacity(num_sections);
4748
let mut sum = 0;
48-
for i in 0..num_sections as usize {
49+
for i in 0..num_sections {
4950
// size 2 bytes 0x0001-0xFFFF
5051
// 16-bit unsigned big-endian integer denoting the length of the section content
5152
let code_size = u16::from_be_bytes([input[i * 2], input[i * 2 + 1]]);
@@ -255,4 +256,23 @@ mod tests {
255256
let input = hex!("ef00010100040200010006030001001404000200008000016000e0000000ef000101000402000100010400000000800000fe");
256257
let _ = EofHeader::decode(&input).unwrap();
257258
}
259+
260+
#[test]
261+
fn short_input() {
262+
let input = hex!("ef0001010000028000");
263+
assert_eq!(
264+
EofHeader::decode(&input),
265+
Err(EofDecodeError::ShortInputForSizes)
266+
);
267+
}
268+
269+
#[test]
270+
fn test_invalid_non_returning_flag() {
271+
let input =
272+
hex!("ef000101000c020003000400010003041d0000008000000080000000000000e300020000e50001");
273+
assert_eq!(
274+
EofHeader::decode(&input),
275+
Err(EofDecodeError::ShortInputForSizes)
276+
);
277+
}
258278
}

0 commit comments

Comments
 (0)