|
1 | 1 | use std::collections::HashMap;
|
2 | 2 | use std::fs::{remove_file, File, OpenOptions};
|
3 | 3 | use std::io::{Read, Write};
|
| 4 | +use std::convert::TryFrom; |
4 | 5 |
|
5 | 6 | use rustc::ty::layout::Size;
|
6 | 7 |
|
@@ -174,16 +175,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
174 | 175 | let buf = this.read_scalar(buf_op)?.not_undef()?;
|
175 | 176 |
|
176 | 177 | if let Some(handle) = this.machine.file_handler.handles.get_mut(&fd) {
|
177 |
| - let count = helpers::try_into_host_usize(count) |
178 |
| - .ok_or_else(|| err_unsup_format!("Program tries to read into buffer too big for this host platform"))?; |
179 |
| - // We want to read at most `count` bytes |
180 |
| - let mut bytes = vec![0; count]; |
| 178 | + let count = isize::try_from(count) |
| 179 | + .map_err(|_| err_unsup_format!("Program tries to read into buffer too big for this host platform"))?; |
| 180 | + // We want to read at most `count` bytes. We are sure that `count` is not negative |
| 181 | + // because it was a target's `usize`. Also we are sure that its smaller than |
| 182 | + // `usize::max_value()` because it is a host's `isize`. |
| 183 | + let mut bytes = vec![0; count as usize]; |
181 | 184 | let result = handle.file.read(&mut bytes);
|
182 | 185 |
|
183 | 186 | match result {
|
184 | 187 | Ok(c) => {
|
185 |
| - let read_bytes = helpers::try_from_host_usize::<i64>(c) |
186 |
| - .ok_or_else(|| err_unsup_format!("Number of read bytes {} cannot be transformed to i64", c))?; |
| 188 | + let read_bytes = i64::try_from(c) |
| 189 | + .map_err(|_| err_unsup_format!("Number of read bytes {} cannot be transformed to i64", c))?; |
187 | 190 | // If reading to `bytes` did not fail, we write those bytes to the buffer.
|
188 | 191 | this.memory.write_bytes(buf, bytes)?;
|
189 | 192 | Ok(read_bytes)
|
@@ -221,8 +224,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
221 | 224 | let result = handle.file.write(&bytes);
|
222 | 225 |
|
223 | 226 | match result {
|
224 |
| - Ok(c) => helpers::try_from_host_usize::<i64>(c) |
225 |
| - .ok_or_else(|| err_unsup_format!("Number of written bytes {} cannot be transformed to i64", c).into()), |
| 227 | + Ok(c) => i64::try_from(c) |
| 228 | + .map_err(|_| err_unsup_format!("Number of written bytes {} cannot be transformed to i64", c).into()), |
226 | 229 | Err(e) => {
|
227 | 230 | this.set_last_error_from_io_error(e)?;
|
228 | 231 | Ok(-1)
|
|
0 commit comments