@@ -590,7 +590,7 @@ fn get_attr_str(buf: &[u8]) -> Option<(&str, &[u8])> {
590590 return Some ( ( "" , & [ ] ) ) ;
591591 }
592592 let len = buf[ 0 ] as usize ;
593- let str = std:: str:: from_utf8 ( & buf[ 1 ..len + 1 ] ) . ok ( ) ?;
593+ let str = std:: str:: from_utf8 ( buf. get ( 1 ..len + 1 ) ? ) . ok ( ) ?;
594594 let remaining_buf = & buf. get ( len + 1 ..) ?;
595595 Some ( ( str, remaining_buf) )
596596}
@@ -1006,7 +1006,34 @@ mod test {
10061006 assert_eq ! ( iter. next( ) , Some ( ( "key" , "value" ) ) ) ;
10071007 while iter. next ( ) . is_some ( ) { } // Consume the iterator
10081008
1009- // Check that iterator ignores non-UTF8 attributes. This behaviour is not
1009+ // Check that invalid len stops the iterator. This behaviour is not perfect as it
1010+ // stops reading attributes after the first invalid one but is just a safety measure.
1011+ // In this case, we set the length byte to 255 which goes beyond the size of the
1012+ // product account.
1013+ product. attr [ 10 ] = 255 ;
1014+ for i in 11 ..266 {
1015+ product. attr [ i] = b'a' ;
1016+ }
1017+ product. attr [ 266 ] = 255 ;
1018+ for i in 267 ..super :: PROD_ATTR_SIZE {
1019+ product. attr [ i] = b'b' ;
1020+ }
1021+ let mut iter = product. iter ( ) ;
1022+ assert_eq ! ( iter. next( ) , Some ( ( "key" , "value" ) ) ) ;
1023+ assert_eq ! ( iter. next( ) , None ) ; // No more attributes because it stopped reading the invalid value
1024+
1025+ // Make sure if the value size was set to a smaller value, it would work fine
1026+ product. attr [ 266 ] = 10 ;
1027+ let mut iter = product. iter ( ) ;
1028+ assert_eq ! ( iter. next( ) , Some ( ( "key" , "value" ) ) ) ;
1029+ let ( key, val) = iter. next ( ) . unwrap ( ) ;
1030+ assert_eq ! ( key. len( ) , 255 ) ;
1031+ for byte in key. as_bytes ( ) {
1032+ assert_eq ! ( byte, & b'a' ) ;
1033+ }
1034+ assert_eq ! ( val, "bbbbbbbbbb" ) ; // No more attributes because it stopped reading the invalid value
1035+
1036+ // Check that iterator stops on non-UTF8 attributes. This behaviour is not
10101037 // perfect as it stops reading attributes after the first non-UTF8 one but
10111038 // is just a safety measure.
10121039 product. attr [ 1 ..4 ] . copy_from_slice ( b"\xff \xfe \xfa " ) ;
0 commit comments