@@ -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