Skip to content

Commit 4bbaa72

Browse files
committed
Use TryFrom instead
1 parent f910ea1 commit 4bbaa72

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/helpers.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{mem, iter};
22
use std::ffi::{OsStr, OsString};
3-
use std::convert::TryFrom;
43

54
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
65
use rustc::mir;
@@ -460,16 +459,3 @@ fn bytes_to_os_str<'tcx, 'a>(bytes: &'a[u8]) -> InterpResult<'tcx, &'a OsStr> {
460459
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
461460
Ok(&OsStr::new(s))
462461
}
463-
464-
pub fn try_into_host_usize(i: impl Into<u128>) -> Option<usize> {
465-
let i: u128 = i.into();
466-
if i > usize::max_value() as u128 {
467-
None
468-
} else {
469-
Some(i as usize)
470-
}
471-
}
472-
473-
pub fn try_from_host_usize<T: TryFrom<u128>>(i: usize) -> Option<T> {
474-
T::try_from(i as u128).ok()
475-
}

src/shims/fs.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22
use std::fs::{remove_file, File, OpenOptions};
33
use std::io::{Read, Write};
4+
use std::convert::TryFrom;
45

56
use rustc::ty::layout::Size;
67

@@ -174,16 +175,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
174175
let buf = this.read_scalar(buf_op)?.not_undef()?;
175176

176177
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];
181184
let result = handle.file.read(&mut bytes);
182185

183186
match result {
184187
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))?;
187190
// If reading to `bytes` did not fail, we write those bytes to the buffer.
188191
this.memory.write_bytes(buf, bytes)?;
189192
Ok(read_bytes)
@@ -221,8 +224,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
221224
let result = handle.file.write(&bytes);
222225

223226
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()),
226229
Err(e) => {
227230
this.set_last_error_from_io_error(e)?;
228231
Ok(-1)

0 commit comments

Comments
 (0)