diff --git a/src/shims/fs.rs b/src/shims/fs.rs index 910e3907695c..3fccbaa33f05 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -174,19 +174,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx 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::(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::(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 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let result = handle.file.write(&bytes); match result { - Ok(c) => { - if let Some(written_bytes) = helpers::try_from_host_usize::(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::(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)