@@ -178,16 +178,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
178
178
let mut bytes = vec ! [ 0 ; count as usize ] ;
179
179
let result = handle. file . read ( & mut bytes) ;
180
180
181
- if let Ok ( c) = result {
182
- // Check that we read less than `i64::MAX` bytes.
183
- if ( c as u64 ) > ( i64:: max_value ( ) as u64 ) {
184
- throw_unsup_format ! ( "Number of read bytes {} is larger than the maximum value" , c) ;
181
+ match result {
182
+ Ok ( c) => {
183
+ if let Some ( read_bytes) = helpers:: try_from_host_usize :: < i64 > ( c) {
184
+ // If reading to `bytes` did not fail, we write those bytes to the buffer.
185
+ this. memory . write_bytes ( buf, bytes) ?;
186
+ Ok ( read_bytes)
187
+ } else {
188
+ throw_unsup_format ! ( "Number of read bytes {} cannot be transformed to i64" , c) ;
189
+ }
190
+ } ,
191
+ Err ( e) => {
192
+ this. set_last_error_from_io_error ( e) ?;
193
+ Ok ( -1 )
185
194
}
186
- // If reading to `bytes` did not fail, we write those bytes to the buffer.
187
- this. memory . write_bytes ( buf, bytes) ?
188
195
}
189
-
190
- this. try_unwrap_io_result ( result. map ( |c| c as i64 ) )
191
196
} else {
192
197
this. handle_not_found ( )
193
198
}
@@ -215,14 +220,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
215
220
let bytes = this. memory . read_bytes ( buf, Size :: from_bytes ( count) ) ?;
216
221
let result = handle. file . write ( & bytes) ;
217
222
218
- if let Ok ( c) = result {
219
- // Check that we wrote less than `i64::MAX` bytes.
220
- if ( c as u64 ) > ( i64:: max_value ( ) as u64 ) {
221
- throw_unsup_format ! ( "Number of written bytes {} is larger than the maximum value" , c) ;
223
+ match result {
224
+ Ok ( c) => {
225
+ if let Some ( written_bytes) = helpers:: try_from_host_usize :: < i64 > ( c) {
226
+ Ok ( written_bytes)
227
+ } else {
228
+ throw_unsup_format ! ( "Number of written bytes {} cannot be transformed to i64" , c) ;
229
+ }
230
+ } ,
231
+ Err ( e) => {
232
+ this. set_last_error_from_io_error ( e) ?;
233
+ Ok ( -1 )
222
234
}
223
235
}
224
-
225
- this. try_unwrap_io_result ( result. map ( |c| c as i64 ) )
226
236
} else {
227
237
this. handle_not_found ( )
228
238
}
0 commit comments