@@ -174,19 +174,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
174
174
let buf = this. read_scalar ( buf_op) ?. not_undef ( ) ?;
175
175
176
176
if let Some ( handle) = this. machine . file_handler . handles . get_mut ( & fd) {
177
+ let count = helpers:: try_into_host_usize ( count)
178
+ . ok_or_else ( || err_unsup_format ! ( "Program tries to read into buffer too big for this host platform" ) ) ?;
177
179
// We want to read at most `count` bytes
178
- let mut bytes = vec ! [ 0 ; count as usize ] ;
180
+ let mut bytes = vec ! [ 0 ; count] ;
179
181
let result = handle. file . read ( & mut bytes) ;
180
182
181
183
match result {
182
184
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
- }
185
+ let read_bytes = helpers:: try_from_host_usize :: < i64 > ( c)
186
+ . ok_or_else ( || err_unsup_format ! ( "Number of read bytes {} cannot be transformed to i64" , c) ) ?;
187
+ // If reading to `bytes` did not fail, we write those bytes to the buffer.
188
+ this. memory . write_bytes ( buf, bytes) ?;
189
+ Ok ( read_bytes)
190
190
} ,
191
191
Err ( e) => {
192
192
this. set_last_error_from_io_error ( e) ?;
@@ -221,13 +221,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
221
221
let result = handle. file . write ( & bytes) ;
222
222
223
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
- } ,
224
+ Ok ( c) => helpers:: try_from_host_usize :: < i64 > ( c)
225
+ . ok_or_else ( || err_unsup_format ! ( "Number of written bytes {} cannot be transformed to i64" , c) . into ( ) ) ,
231
226
Err ( e) => {
232
227
this. set_last_error_from_io_error ( e) ?;
233
228
Ok ( -1 )
0 commit comments