]> git.lizzy.rs Git - rust.git/commitdiff
Cap `count` twice
authorChristian Poveda <christianpoveda@protonmail.com>
Wed, 13 Nov 2019 16:57:20 +0000 (11:57 -0500)
committerChristian Poveda <christianpoveda@protonmail.com>
Wed, 13 Nov 2019 16:57:20 +0000 (11:57 -0500)
src/shims/fs.rs

index 5e84cdee5cc4b0b1ac8819d9f45e0256f2adca55..9a819cd9db4b25200420559e5f89cccc0ee996d7 100644 (file)
@@ -168,10 +168,13 @@ fn read(
 
         let ptr_size = this.pointer_size().bits();
 
+        // We cap the number of read bytes to the largest value that we are able to fit in both the
+        // host's and target's `isize`.
         let count = this
             .read_scalar(count_op)?
             .to_machine_usize(&*this.tcx)?
-            .min(1 << (ptr_size - 1));
+            .min(1 << (ptr_size - 1))
+            .min(isize::max_value() as u64);
         // Reading zero bytes should not change `buf`.
         if count == 0 {
             return Ok(0);
@@ -180,6 +183,8 @@ fn read(
         let buf = this.read_scalar(buf_op)?.not_undef()?;
 
         if let Some(handle) = this.machine.file_handler.handles.get_mut(&fd) {
+            // This can never fail because `count` was capped to be smaller than
+            // `isize::max_value()`.
             let count = isize::try_from(count).unwrap();
             // We want to read at most `count` bytes. We are sure that `count` is not negative
             // because it was a target's `usize`. Also we are sure that its smaller than
@@ -188,6 +193,8 @@ fn read(
             let result = handle
                 .file
                 .read(&mut bytes)
+                // `File::read` never returns a value larger than `i64::max_value()`, so this
+                // unwrap cannot fail.
                 .map(|c| i64::try_from(c).unwrap());
 
             match result {
@@ -218,10 +225,13 @@ fn write(
 
         let ptr_size = this.pointer_size().bits();
 
+        // We cap the number of read bytes to the largest value that we are able to fit in both the
+        // host's and target's `isize`.
         let count = this
             .read_scalar(count_op)?
             .to_machine_usize(&*this.tcx)?
-            .min(1 << (ptr_size - 1));
+            .min(1 << (ptr_size - 1))
+            .min(isize::max_value() as u64);
         // Writing zero bytes should not change `buf`.
         if count == 0 {
             return Ok(0);