]> git.lizzy.rs Git - rust.git/commitdiff
Avoid using `as` cast
authorChristian Poveda <christianpoveda@protonmail.com>
Tue, 5 Nov 2019 21:47:24 +0000 (16:47 -0500)
committerChristian Poveda <christianpoveda@protonmail.com>
Tue, 5 Nov 2019 21:47:24 +0000 (16:47 -0500)
src/shims/fs.rs

index 910e3907695c4aacd2c91b93aa0476591523b53c..3fccbaa33f052845ef437dec7fb55fb869cea985 100644 (file)
@@ -174,19 +174,19 @@ fn read(
         let buf = this.read_scalar(buf_op)?.not_undef()?;
 
         if let Some(handle) = this.machine.file_handler.handles.get_mut(&fd) {
+            let count = helpers::try_into_host_usize(count)
+                .ok_or_else(|| err_unsup_format!("Program tries to read into buffer too big for this host platform"))?;
             // We want to read at most `count` bytes
-            let mut bytes = vec![0; count as usize];
+            let mut bytes = vec![0; count];
             let result = handle.file.read(&mut bytes);
 
             match result {
                 Ok(c) => {
-                    if let Some(read_bytes) = helpers::try_from_host_usize::<i64>(c) {
-                        // If reading to `bytes` did not fail, we write those bytes to the buffer.
-                        this.memory.write_bytes(buf, bytes)?;
-                        Ok(read_bytes)
-                    } else {
-                        throw_unsup_format!("Number of read bytes {} cannot be transformed to i64", c);
-                    }
+                    let read_bytes = helpers::try_from_host_usize::<i64>(c)
+                        .ok_or_else(|| err_unsup_format!("Number of read bytes {} cannot be transformed to i64", c))?;
+                    // If reading to `bytes` did not fail, we write those bytes to the buffer.
+                    this.memory.write_bytes(buf, bytes)?;
+                    Ok(read_bytes)
                 },
                 Err(e) => {
                     this.set_last_error_from_io_error(e)?;
@@ -221,13 +221,8 @@ fn write(
             let result = handle.file.write(&bytes);
 
             match result {
-                Ok(c) => {
-                    if let Some(written_bytes) = helpers::try_from_host_usize::<i64>(c) {
-                        Ok(written_bytes)
-                    } else {
-                        throw_unsup_format!("Number of written bytes {} cannot be transformed to i64", c);
-                    }
-                },
+                Ok(c) => helpers::try_from_host_usize::<i64>(c)
+                    .ok_or_else(|| err_unsup_format!("Number of written bytes {} cannot be transformed to i64", c).into()),
                 Err(e) => {
                     this.set_last_error_from_io_error(e)?;
                     Ok(-1)