Skip to content

Commit 50be5a8

Browse files
committed
Remove return argument when fd is not found
1 parent 644467c commit 50be5a8

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/shims/io.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5050
.memory()
5151
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
5252
let path = std::str::from_utf8(path_bytes)
53-
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?
54-
.to_owned();
55-
let fd = File::open(&path).map(|file| {
53+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
54+
let fd = File::open(path).map(|file| {
5655
let mut fh = &mut this.machine.file_handler;
5756
fh.low += 1;
5857
fh.handles.insert(fh.low, FileHandle { file, flag });
5958
fh.low
6059
});
6160

62-
this.consume_result::<i32>(fd, -1)
61+
this.consume_result(fd)
6362
}
6463

6564
fn fcntl(
@@ -94,7 +93,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9493
}
9594
Ok(0)
9695
} else if cmd == this.eval_libc_i32("F_GETFD")? {
97-
this.get_handle_and(fd, |handle| Ok(handle.flag), -1)
96+
this.get_handle_and(fd, |handle| Ok(handle.flag))
9897
} else {
9998
throw_unsup_format!("Unsupported command {:#x}", cmd);
10099
}
@@ -111,8 +110,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
111110

112111
this.remove_handle_and(
113112
fd,
114-
|handle, this| this.consume_result::<i32>(handle.file.sync_all().map(|_| 0), -1),
115-
-1,
113+
|handle, this| this.consume_result(handle.file.sync_all().map(|_| 0i32)),
116114
)
117115
}
118116

@@ -148,13 +146,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
148146
.map(|bytes| bytes as i64);
149147
// Reinsert the file handle
150148
this.machine.file_handler.handles.insert(fd, handle);
151-
this.consume_result::<i64>(bytes, -1)
149+
this.consume_result(bytes)
152150
},
153-
-1,
154151
)
155152
}
156153

157-
fn get_handle_and<F, T>(&mut self, fd: i32, f: F, t: T) -> InterpResult<'tcx, T>
154+
fn get_handle_and<F, T: From<i32>>(&mut self, fd: i32, f: F) -> InterpResult<'tcx, T>
158155
where
159156
F: Fn(&FileHandle) -> InterpResult<'tcx, T>,
160157
{
@@ -163,11 +160,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
163160
f(handle)
164161
} else {
165162
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
166-
Ok(t)
163+
Ok((-1).into())
167164
}
168165
}
169166

170-
fn remove_handle_and<F, T>(&mut self, fd: i32, mut f: F, t: T) -> InterpResult<'tcx, T>
167+
fn remove_handle_and<F, T: From<i32>>(&mut self, fd: i32, mut f: F) -> InterpResult<'tcx, T>
171168
where
172169
F: FnMut(FileHandle, &mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx, T>,
173170
{
@@ -176,16 +173,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
176173
f(handle, this)
177174
} else {
178175
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
179-
Ok(t)
176+
Ok((-1).into())
180177
}
181178
}
182179

183-
fn consume_result<T>(&mut self, result: std::io::Result<T>, t: T) -> InterpResult<'tcx, T> {
180+
fn consume_result<T: From<i32>>(&mut self, result: std::io::Result<T>) -> InterpResult<'tcx, T> {
184181
match result {
185182
Ok(ok) => Ok(ok),
186183
Err(e) => {
187184
self.eval_context_mut().machine.last_error = e.raw_os_error().unwrap() as u32;
188-
Ok(t)
185+
Ok((-1).into())
189186
}
190187
}
191188
}

0 commit comments

Comments
 (0)