@@ -375,6 +375,11 @@ impl<'a> NameIndex<'a> {
375
375
// This is a total hack, because strtab returns "" if idx == 0, need to change
376
376
// but previous behavior might rely on this, as ELF strtab's have "" at 0th index...
377
377
let hacked_size = size + 1 ;
378
+ if hacked_size < 2 {
379
+ return Err ( Error :: Malformed ( format ! (
380
+ "Size ({hacked_size:#x}) too small"
381
+ ) ) ) ;
382
+ }
378
383
let strtab = strtab:: Strtab :: parse ( buffer, * offset - 1 , hacked_size, b'\n' ) ?;
379
384
// precious time was lost when refactoring because strtab::parse doesn't update the mutable seek...
380
385
* offset += hacked_size - 2 ;
@@ -639,6 +644,19 @@ mod tests {
639
644
assert_eq ! ( Member :: bsd_filename_length( "#1/1 A" ) , None ) ;
640
645
}
641
646
647
+ /// https://github.com/m4b/goblin/issues/450
648
+ const MALFORMED_ARCHIVE_INDEX_TOO_SMALL : [ u8 ; 132 ] = [
649
+ 0x21 , 0x3C , 0x61 , 0x72 , 0x63 , 0x68 , 0x3E , 0x0A , 0x55 , 0x52 , 0x09 , 0x5C , 0x09 , 0x09 , 0x10 ,
650
+ 0x27 , 0x2B , 0x09 , 0x0A , 0x53 , 0x54 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 ,
651
+ 0x09 , 0x09 , 0x09 , 0x09 , 0x2A , 0x29 , 0x2A , 0x09 , 0xF7 , 0x08 , 0x09 , 0x09 , 0x00 , 0x01 , 0x01 ,
652
+ 0x01 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x01 , 0x00 , 0x31 , 0x20 , 0x20 , 0x20 ,
653
+ 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x08 , 0x2F , 0x2F , 0x20 , 0x20 , 0x20 ,
654
+ 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x09 ,
655
+ 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x23 , 0x42 , 0x21 , 0x09 , 0xF7 , 0x08 , 0x20 , 0x20 , 0x00 ,
656
+ 0x3C , 0x20 , 0x20 , 0x20 , 0x00 , 0x20 , 0x20 , 0x20 , 0x20 , 0x09 , 0x09 , 0x01 , 0x01 , 0x30 , 0x0D ,
657
+ 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x09 , 0x00 , 0x00 , 0x27 , 0x55 ,
658
+ ] ;
659
+
642
660
/// https://github.com/m4b/goblin/issues/450
643
661
const MALFORMED_ARCHIVE : [ u8 ; 212 ] = [
644
662
0x21 , 0x3C , 0x61 , 0x72 , 0x63 , 0x68 , 0x3E , 0x0A , 0x2F , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 , 0x20 ,
@@ -658,6 +676,17 @@ mod tests {
658
676
0x14 , 0x34 ,
659
677
] ;
660
678
679
+ #[ test]
680
+ fn parse_name_index_too_small ( ) {
681
+ let res = Archive :: parse ( & MALFORMED_ARCHIVE_INDEX_TOO_SMALL ) ;
682
+ assert_eq ! ( res. is_err( ) , true ) ;
683
+ if let Err ( Error :: Malformed ( msg) ) = res {
684
+ assert_eq ! ( msg, "Size (0x1) too small" ) ;
685
+ } else {
686
+ panic ! ( "Expected a Malformed error but got {:?}" , res) ;
687
+ }
688
+ }
689
+
661
690
#[ test]
662
691
fn parse_malformed_archive ( ) {
663
692
let res = Archive :: parse ( & MALFORMED_ARCHIVE ) ;
0 commit comments