Skip to content

Commit 0ca00b3

Browse files
committed
don't halt execution when we write to a read-only file
1 parent ea405ae commit 0ca00b3

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/shims/files.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ impl FileDescription for FileHandle {
334334
) -> InterpResult<'tcx> {
335335
assert!(communicate_allowed, "isolation should have prevented even opening a file");
336336

337+
if !self.writable {
338+
// Linux returns EBADF here which we can't emulate via the platform-independent
339+
// code since it does not map to any `io::ErrorKind`. So let's hard-code this.
340+
if ecx.target_os_is_unix() {
341+
return finish.call(ecx, Err(IoError::LibcError("EBADF")));
342+
}
343+
}
337344
let result = ecx.write_to_host(&self.file, len, ptr)?;
338345
finish.call(ecx, result)
339346
}

tests/pass/shims/fs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ fn test_file() {
6767

6868
assert!(!file.is_terminal());
6969

70+
// Writing to a file opened for reading should error (and not stop interpretation). std does not
71+
// categorize the error so we don't check for details.
72+
file.write(&[]).unwrap_err();
73+
7074
// Removing file should succeed.
7175
remove_file(&path).unwrap();
7276
}

0 commit comments

Comments
 (0)