@@ -176,12 +176,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
176
176
if let Some ( handle) = this. machine . file_handler . handles . get_mut ( & fd) {
177
177
// We want to read at most `count` bytes
178
178
let mut bytes = vec ! [ 0 ; count as usize ] ;
179
- let result = handle. file . read ( & mut bytes) . map ( |c| c as i64 ) ;
180
- // If reading to `bytes` did not fail, we write those bytes to the buffer.
181
- if result. is_ok ( ) {
182
- this. memory . write_bytes ( buf, bytes) ?;
179
+ let result = handle. file . read ( & mut bytes) ;
180
+
181
+ if let Ok ( c) = result {
182
+ // Check that we read less than `i64::MAX` bytes.
183
+ if c > ( i64:: max_value ( ) as usize ) {
184
+ throw_unsup_format ! ( "Number of read bytes {} is larger than the maximum value" , c) ;
185
+ }
186
+ // If reading to `bytes` did not fail, we write those bytes to the buffer.
187
+ this. memory . write_bytes ( buf, bytes) ?
183
188
}
184
- this. try_unwrap_io_result ( result)
189
+
190
+ this. try_unwrap_io_result ( result. map ( |c| c as i64 ) )
185
191
} else {
186
192
this. handle_not_found ( )
187
193
}
@@ -207,8 +213,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
207
213
208
214
if let Some ( handle) = this. machine . file_handler . handles . get_mut ( & fd) {
209
215
let bytes = this. memory . read_bytes ( buf, Size :: from_bytes ( count) ) ?;
210
- let result = handle. file . write ( & bytes) . map ( |c| c as i64 ) ;
211
- this. try_unwrap_io_result ( result)
216
+ let result = handle. file . write ( & bytes) ;
217
+
218
+ if let Ok ( c) = result {
219
+ // Check that we wrote less than `i64::MAX` bytes.
220
+ if c > ( i64:: max_value ( ) as usize ) {
221
+ throw_unsup_format ! ( "Number of written bytes {} is larger than the maximum value" , c) ;
222
+ }
223
+ }
224
+
225
+ this. try_unwrap_io_result ( result. map ( |c| c as i64 ) )
212
226
} else {
213
227
this. handle_not_found ( )
214
228
}
0 commit comments