]> git.lizzy.rs Git - rust.git/commitdiff
Check for usize to i64 overflows
authorChristian Poveda <christianpoveda@protonmail.com>
Mon, 28 Oct 2019 21:44:18 +0000 (16:44 -0500)
committerChristian Poveda <christianpoveda@protonmail.com>
Mon, 28 Oct 2019 21:44:18 +0000 (16:44 -0500)
src/shims/fs.rs

index e8c42eb9a301771b4e8df7126cf75eac34890931..2710cbacb0213e90cfacfc14c9d06297d13ff52f 100644 (file)
@@ -176,12 +176,18 @@ fn read(
         if let Some(handle) = this.machine.file_handler.handles.get_mut(&fd) {
             // We want to read at most `count` bytes
             let mut bytes = vec![0; count as usize];
-            let result = handle.file.read(&mut bytes).map(|c| c as i64);
-            // If reading to `bytes` did not fail, we write those bytes to the buffer.
-            if result.is_ok() {
-                this.memory.write_bytes(buf, bytes)?;
+            let result = handle.file.read(&mut bytes);
+
+            if let Ok(c) = result {
+                // Check that we read less than `i64::MAX` bytes.
+                if c > (i64::max_value() as usize) {
+                    throw_unsup_format!("Number of read bytes {} is larger than the maximum value", c);
+                }
+                // If reading to `bytes` did not fail, we write those bytes to the buffer.
+                this.memory.write_bytes(buf, bytes)?
             }
-            this.try_unwrap_io_result(result)
+
+            this.try_unwrap_io_result(result.map(|c| c as i64))
         } else {
             this.handle_not_found()
         }
@@ -207,8 +213,16 @@ fn write(
 
         if let Some(handle) = this.machine.file_handler.handles.get_mut(&fd) {
             let bytes = this.memory.read_bytes(buf, Size::from_bytes(count))?;
-            let result = handle.file.write(&bytes).map(|c| c as i64);
-            this.try_unwrap_io_result(result)
+            let result = handle.file.write(&bytes);
+
+            if let Ok(c) = result {
+                // Check that we wrote less than `i64::MAX` bytes.
+                if c > (i64::max_value() as usize) {
+                    throw_unsup_format!("Number of written bytes {} is larger than the maximum value", c);
+                }
+            }
+
+            this.try_unwrap_io_result(result.map(|c| c as i64))
         } else {
             this.handle_not_found()
         }