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)?;
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)