Skip to content

Commit dc4b8ac

Browse files
committed
close file silently if the file is read only
1 parent 0f1dec0 commit dc4b8ac

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/shims/fs.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use shims::time::system_time_to_duration;
1515
#[derive(Debug)]
1616
pub struct FileHandle {
1717
file: File,
18+
read_only: bool,
1819
}
1920

2021
pub struct FileHandler {
@@ -56,10 +57,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5657
if (o_rdonly | o_wronly | o_rdwr) & !0b11 != 0 {
5758
throw_unsup_format!("Access mode flags on this platform are unsupported");
5859
}
60+
let mut read_only = false;
61+
5962
// Now we check the access mode
6063
let access_mode = flag & 0b11;
6164

6265
if access_mode == o_rdonly {
66+
read_only = true;
6367
options.read(true);
6468
} else if access_mode == o_wronly {
6569
options.write(true);
@@ -105,7 +109,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
105109
let fd = options.open(&path).map(|file| {
106110
let mut fh = &mut this.machine.file_handler;
107111
fh.low += 1;
108-
fh.handles.insert(fh.low, FileHandle { file }).unwrap_none();
112+
fh.handles.insert(fh.low, FileHandle { file, read_only }).unwrap_none();
109113
fh.low
110114
});
111115

@@ -148,6 +152,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
148152
let fd = this.read_scalar(fd_op)?.to_i32()?;
149153

150154
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
155+
if handle.read_only {
156+
return Ok(0);
157+
}
151158
// `File::sync_all` does the checks that are done when closing a file. We do this to
152159
// to handle possible errors correctly.
153160
let result = this.try_unwrap_io_result(handle.file.sync_all().map(|_| 0i32));

0 commit comments

Comments
 (0)