Skip to content

Commit ce4e1f9

Browse files
committed
add comments
1 parent f00fd39 commit ce4e1f9

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/shims/fs.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
152152
let fd = this.read_scalar(fd_op)?.to_i32()?;
153153

154154
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
155-
if handle.read_only {
156-
return Ok(0);
155+
// We sync the file if it was opened in a mode different than read-only.
156+
if !handle.read_only {
157+
// `File::sync_all` does the checks that are done when closing a file. We do this to
158+
// to handle possible errors correctly.
159+
let result = this.try_unwrap_io_result(handle.file.sync_all().map(|_| 0i32));
160+
// Now we actually close the file.
161+
drop(handle);
162+
// And return the result.
163+
result
164+
} else {
165+
// We drop the file, this closes it but ignores any errors produced when closing
166+
// it. This is done because `File::sync_call` cannot be done over files like
167+
// `/dev/urandom`. Check
168+
// https://github.com/rust-lang/miri/issues/999#issuecomment-568920439 for a deeper
169+
// discussion.
170+
drop(handle);
171+
Ok(0)
157172
}
158-
// `File::sync_all` does the checks that are done when closing a file. We do this to
159-
// to handle possible errors correctly.
160-
let result = this.try_unwrap_io_result(handle.file.sync_all().map(|_| 0i32));
161-
// Now we actually close the file.
162-
drop(handle);
163-
// And return the result.
164-
result
165173
} else {
166174
this.handle_not_found()
167175
}

0 commit comments

Comments
 (0)