Skip to content

Commit bb6eb71

Browse files
committed
don't halt execution when we write to a read-only file
1 parent 9675a3d commit bb6eb71

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/shims/files.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::any::Any;
22
use std::collections::BTreeMap;
33
use std::fs::{File, Metadata};
4-
use std::io::{IsTerminal, Seek, SeekFrom, Write};
4+
use std::io::{ErrorKind, IsTerminal, Seek, SeekFrom, Write};
55
use std::marker::CoercePointee;
66
use std::ops::Deref;
77
use std::rc::{Rc, Weak};
@@ -334,6 +334,15 @@ 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 hosts return EBADF here which we can't translate via the platform-independent
339+
// code since it does not map to any `io::ErrorKind` -- so if we don't do anything
340+
// special, we'd throw an "unsupported error code" here. Windows returns something that
341+
// gets translated to `PermissionDenied`. That seems like a good value so let's just use
342+
// this everywhere, even if it means behavior on Unix targets does not match the real
343+
// thing.
344+
return finish.call(ecx, Err(ErrorKind::PermissionDenied.into()));
345+
}
337346
let result = ecx.write_to_host(&self.file, len, ptr)?;
338347
finish.call(ecx, result)
339348
}

tests/pass/shims/fs.rs

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

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

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

0 commit comments

Comments
 (0)