@@ -149,25 +149,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
149
149
150
150
let tcx = & { this. tcx . tcx } ;
151
151
152
+ let count = this. read_scalar ( count_op) ?. to_usize ( & * this. tcx ) ?;
153
+ // Reading zero bytes should not change `buf`
154
+ if count == 0 {
155
+ return Ok ( 0 ) ;
156
+ }
152
157
let fd = this. read_scalar ( fd_op) ?. to_i32 ( ) ?;
153
158
let buf_scalar = this. read_scalar ( buf_op) ?. not_undef ( ) ?;
154
- let count = this. read_scalar ( count_op) ?. to_usize ( & * this. tcx ) ?;
155
159
156
160
// Remove the file handle to avoid borrowing issues
157
161
this. remove_handle_and ( fd, |mut handle, this| {
158
162
// Don't use `?` to avoid returning before reinserting the handle
159
- let bytes =
160
- if count == 0 {
161
- Ok ( handle. file . read ( & mut [ ] ) )
162
- } else {
163
- this. force_ptr ( buf_scalar) . and_then ( |buf| this
164
- . memory_mut ( )
165
- . get_mut ( buf. alloc_id ) . and_then ( |alloc|
166
- alloc. get_bytes_mut ( tcx, buf, Size :: from_bytes ( count) )
167
- . map ( |buffer| handle. file . read ( buffer) )
168
- ) )
169
-
170
- } ;
163
+ let bytes = this. force_ptr ( buf_scalar) . and_then ( |buf| {
164
+ this. memory_mut ( )
165
+ . get_mut ( buf. alloc_id ) ?
166
+ . get_bytes_mut ( tcx, buf, Size :: from_bytes ( count) )
167
+ . map ( |buffer| handle. file . read ( buffer) )
168
+ } ) ;
171
169
// Reinsert the file handle
172
170
this. machine . file_handler . handles . insert ( fd, handle) ;
173
171
this. consume_result ( bytes?. map ( |bytes| bytes as i64 ) )
@@ -188,9 +186,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
188
186
189
187
let tcx = & { this. tcx . tcx } ;
190
188
189
+ let count = this. read_scalar ( count_op) ?. to_usize ( & * this. tcx ) ?;
190
+ // Writing zero bytes should not change `buf`
191
+ if count == 0 {
192
+ return Ok ( 0 ) ;
193
+ }
191
194
let fd = this. read_scalar ( fd_op) ?. to_i32 ( ) ?;
192
195
let buf = this. force_ptr ( this. read_scalar ( buf_op) ?. not_undef ( ) ?) ?;
193
- let count = this. read_scalar ( count_op) ?. to_usize ( & * this. tcx ) ?;
194
196
195
197
this. remove_handle_and ( fd, |mut handle, this| {
196
198
let bytes = this. memory ( ) . get ( buf. alloc_id ) . and_then ( |alloc| {
0 commit comments