From dc4b8ac2e65d80bbe68893fd8ab409c7e86187ec Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 27 Dec 2019 08:34:35 -0500 Subject: [PATCH] close file silently if the file is read only --- src/shims/fs.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/shims/fs.rs b/src/shims/fs.rs index 63aa750bdd54..c6cae22331d1 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -15,6 +15,7 @@ use shims::time::system_time_to_duration; #[derive(Debug)] pub struct FileHandle { file: File, + read_only: bool, } pub struct FileHandler { @@ -56,10 +57,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx if (o_rdonly | o_wronly | o_rdwr) & !0b11 != 0 { throw_unsup_format!("Access mode flags on this platform are unsupported"); } + let mut read_only = false; + // Now we check the access mode let access_mode = flag & 0b11; if access_mode == o_rdonly { + read_only = true; options.read(true); } else if access_mode == o_wronly { options.write(true); @@ -105,7 +109,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let fd = options.open(&path).map(|file| { let mut fh = &mut this.machine.file_handler; fh.low += 1; - fh.handles.insert(fh.low, FileHandle { file }).unwrap_none(); + fh.handles.insert(fh.low, FileHandle { file, read_only }).unwrap_none(); fh.low }); @@ -148,6 +152,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let fd = this.read_scalar(fd_op)?.to_i32()?; if let Some(handle) = this.machine.file_handler.handles.remove(&fd) { + if handle.read_only { + return Ok(0); + } // `File::sync_all` does the checks that are done when closing a file. We do this to // to handle possible errors correctly. let result = this.try_unwrap_io_result(handle.file.sync_all().map(|_| 0i32));