File tree 1 file changed +21
-3
lines changed
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(
43
43
// The implementation here relies on FILE_HEADER_SIZE to have the value 8.
44
44
// Let's make sure this assumption cannot be violated without being noticed.
45
45
assert_eq ! ( FILE_HEADER_SIZE , 8 ) ;
46
- assert ! ( bytes. len( ) >= FILE_HEADER_SIZE ) ;
47
-
48
- let actual_magic = & bytes[ 0 ..4 ] ;
49
46
50
47
let diagnostic_file_path = diagnostic_file_path. unwrap_or ( Path :: new ( "<in-memory>" ) ) ;
51
48
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
+
52
63
if actual_magic != expected_magic {
53
64
let msg = format ! (
54
65
"Error reading {} stream in file `{}`: Expected file magic `{:?}` but found `{:?}`" ,
@@ -124,4 +135,11 @@ mod tests {
124
135
data[ 7 ] = 0xFF ;
125
136
assert ! ( verify_file_header( & data, FILE_MAGIC_STRINGTABLE_INDEX , None , "test" ) . is_err( ) ) ;
126
137
}
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
+ }
127
145
}
You can’t perform that action at this time.
0 commit comments