Skip to content

Commit

Permalink
fix(EOF): Overflow on num_sections (#1656)
Browse files Browse the repository at this point in the history
* fix(EOF): Overflow on num_sections

* fix test

* fmt/clippy
  • Loading branch information
rakita authored Jul 24, 2024
1 parent afb8083 commit d71acb5
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions crates/primitives/src/bytecode/eof/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ fn consume_header_section_size(input: &[u8]) -> Result<(&[u8], Vec<u16>, usize),
if num_sections == 0 {
return Err(EofDecodeError::NonSizes);
}
let byte_size = (num_sections * 2) as usize;
let num_sections = num_sections as usize;
let byte_size = num_sections * 2;
if input.len() < byte_size {
return Err(EofDecodeError::ShortInputForSizes);
}
let mut sizes = Vec::with_capacity(num_sections as usize);
let mut sizes = Vec::with_capacity(num_sections);
let mut sum = 0;
for i in 0..num_sections as usize {
for i in 0..num_sections {
// size 2 bytes 0x0001-0xFFFF
// 16-bit unsigned big-endian integer denoting the length of the section content
let code_size = u16::from_be_bytes([input[i * 2], input[i * 2 + 1]]);
Expand Down Expand Up @@ -255,4 +256,23 @@ mod tests {
let input = hex!("ef00010100040200010006030001001404000200008000016000e0000000ef000101000402000100010400000000800000fe");
let _ = EofHeader::decode(&input).unwrap();
}

#[test]
fn short_input() {
let input = hex!("ef0001010000028000");
assert_eq!(
EofHeader::decode(&input),
Err(EofDecodeError::ShortInputForSizes)
);
}

#[test]
fn test_invalid_non_returning_flag() {
let input =
hex!("ef000101000c020003000400010003041d0000008000000080000000000000e300020000e50001");
assert_eq!(
EofHeader::decode(&input),
Err(EofDecodeError::ShortInputForSizes)
);
}
}

0 comments on commit d71acb5

Please sign in to comment.