]> git.lizzy.rs Git - rust.git/commitdiff
Read into buffer of fixed size for `read`s to STDIN
authorSamrat Man Singh <samratmansingh@gmail.com>
Sun, 12 Jul 2020 14:57:19 +0000 (20:27 +0530)
committerSamrat Man Singh <samratmansingh@gmail.com>
Sun, 12 Jul 2020 14:57:19 +0000 (20:27 +0530)
Also:
- Check isolation is disabled.
- Add FIXMEs to set error numbers in `read` and `write`.

src/shims/posix/foreign_items.rs

index 8efe8f0ed3fd1de441805b5e1ddd1269a0552b1b..200d50433b3ee57452d2d39cfa343fb5a43086aa 100644 (file)
@@ -69,14 +69,21 @@ fn emulate_foreign_item_by_name(
                 let result = if fd == 0 {
                     use std::io::{self, Read};
 
-                    let mut buffer = String::new();
-                    let res = io::stdin().read_to_string(&mut buffer);
+                    this.check_no_isolation("read")?;
+
+                    let mut buffer = vec![0; count as usize];
+                    let res = io::stdin()
+                        .read(&mut buffer)
+                        // `Stdin::read` never returns a value larger
+                        // than `count`, so this cannot fail.
+                        .map(|c| i64::try_from(c).unwrap());
 
                     match res {
                         Ok(bytes) => {
-                            this.memory.write_bytes(buf, buffer.bytes())?;
+                            this.memory.write_bytes(buf, buffer)?;
                             i64::try_from(bytes).unwrap()
                         },
+                        // FIXME: set errno to appropriate value
                         Err(_) => -1,
                     }
                 } else if fd == 1 || fd == 2 {
@@ -114,6 +121,7 @@ fn emulate_foreign_item_by_name(
                     };
                     match res {
                         Ok(n) => i64::try_from(n).unwrap(),
+                        // FIXME: set errno to appropriate value
                         Err(_) => -1,
                     }
                 } else {