Skip to content

Commit 74ff4f8

Browse files
committed
Read into buffer of fixed size for reads to STDIN
Also: - Check isolation is disabled. - Add FIXMEs to set error numbers in `read` and `write`.
1 parent 2602e95 commit 74ff4f8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/shims/posix/foreign_items.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6969
let result = if fd == 0 {
7070
use std::io::{self, Read};
7171

72-
let mut buffer = String::new();
73-
let res = io::stdin().read_to_string(&mut buffer);
72+
this.check_no_isolation("read")?;
73+
74+
let mut buffer = vec![0; count as usize];
75+
let res = io::stdin()
76+
.read(&mut buffer)
77+
// `Stdin::read` never returns a value larger
78+
// than `count`, so this cannot fail.
79+
.map(|c| i64::try_from(c).unwrap());
7480

7581
match res {
7682
Ok(bytes) => {
77-
this.memory.write_bytes(buf, buffer.bytes())?;
83+
this.memory.write_bytes(buf, buffer)?;
7884
i64::try_from(bytes).unwrap()
7985
},
86+
// FIXME: set errno to appropriate value
8087
Err(_) => -1,
8188
}
8289
} else if fd == 1 || fd == 2 {
@@ -114,6 +121,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
114121
};
115122
match res {
116123
Ok(n) => i64::try_from(n).unwrap(),
124+
// FIXME: set errno to appropriate value
117125
Err(_) => -1,
118126
}
119127
} else {

0 commit comments

Comments
 (0)