Skip to content

Commit f910ea1

Browse files
committed
Avoid using as cast
1 parent 56c5e53 commit f910ea1

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/shims/fs.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
174174
let buf = this.read_scalar(buf_op)?.not_undef()?;
175175

176176
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"))?;
177179
// We want to read at most `count` bytes
178-
let mut bytes = vec![0; count as usize];
180+
let mut bytes = vec![0; count];
179181
let result = handle.file.read(&mut bytes);
180182

181183
match result {
182184
Ok(c) => {
183-
if let Some(read_bytes) = helpers::try_from_host_usize::<i64>(c) {
184-
// If reading to `bytes` did not fail, we write those bytes to the buffer.
185-
this.memory.write_bytes(buf, bytes)?;
186-
Ok(read_bytes)
187-
} else {
188-
throw_unsup_format!("Number of read bytes {} cannot be transformed to i64", c);
189-
}
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))?;
187+
// If reading to `bytes` did not fail, we write those bytes to the buffer.
188+
this.memory.write_bytes(buf, bytes)?;
189+
Ok(read_bytes)
190190
},
191191
Err(e) => {
192192
this.set_last_error_from_io_error(e)?;
@@ -221,13 +221,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
221221
let result = handle.file.write(&bytes);
222222

223223
match result {
224-
Ok(c) => {
225-
if let Some(written_bytes) = helpers::try_from_host_usize::<i64>(c) {
226-
Ok(written_bytes)
227-
} else {
228-
throw_unsup_format!("Number of written bytes {} cannot be transformed to i64", c);
229-
}
230-
},
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()),
231226
Err(e) => {
232227
this.set_last_error_from_io_error(e)?;
233228
Ok(-1)

0 commit comments

Comments
 (0)