File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -43,12 +43,23 @@ pub fn verify_file_header(
4343 // The implementation here relies on FILE_HEADER_SIZE to have the value 8.
4444 // Let's make sure this assumption cannot be violated without being noticed.
4545 assert_eq ! ( FILE_HEADER_SIZE , 8 ) ;
46- assert ! ( bytes. len( ) >= FILE_HEADER_SIZE ) ;
47-
48- let actual_magic = & bytes[ 0 ..4 ] ;
4946
5047 let diagnostic_file_path = diagnostic_file_path. unwrap_or ( Path :: new ( "<in-memory>" ) ) ;
5148
49+ if bytes. len ( ) < FILE_HEADER_SIZE {
50+ let msg = format ! (
51+ "Error reading {} stream in file `{}`: Expected file to contain at least `{:?}` bytes but found `{:?}` bytes" ,
52+ stream_tag,
53+ diagnostic_file_path. display( ) ,
54+ FILE_HEADER_SIZE ,
55+ bytes. len( )
56+ ) ;
57+
58+ return Err ( From :: from ( msg) ) ;
59+ }
60+
61+ let actual_magic = & bytes[ 0 ..4 ] ;
62+
5263 if actual_magic != expected_magic {
5364 let msg = format ! (
5465 "Error reading {} stream in file `{}`: Expected file magic `{:?}` but found `{:?}`" ,
@@ -124,4 +135,11 @@ mod tests {
124135 data[ 7 ] = 0xFF ;
125136 assert ! ( verify_file_header( & data, FILE_MAGIC_STRINGTABLE_INDEX , None , "test" ) . is_err( ) ) ;
126137 }
138+
139+ #[ test]
140+ fn empty_file ( ) {
141+ let data: [ u8 ; 0 ] = [ ] ;
142+
143+ assert ! ( verify_file_header( & data, FILE_MAGIC_STRINGTABLE_DATA , None , "test" ) . is_err( ) ) ;
144+ }
127145}
You can’t perform that action at this time.
0 commit comments