Skip to content

Commit 007573b

Browse files
committed
add set_last_error_and_return_i32 helper and use it in a few places
1 parent c49845d commit 007573b

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/shims/io_error.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
119119
this.write_scalar(errno, &errno_place)
120120
}
121121

122+
/// Sets the last OS error and writes -1 to dest place.
123+
fn set_last_error_and_return(
124+
&mut self,
125+
err: impl Into<IoError>,
126+
dest: &MPlaceTy<'tcx>,
127+
) -> InterpResult<'tcx> {
128+
let this = self.eval_context_mut();
129+
this.set_last_error(err)?;
130+
this.write_int(-1, dest)?;
131+
Ok(())
132+
}
133+
134+
/// Sets the last OS error and return `-1` as a `i32`-typed Scalar
135+
fn set_last_error_and_return_i32(
136+
&mut self,
137+
err: impl Into<IoError>,
138+
) -> InterpResult<'tcx, Scalar> {
139+
let this = self.eval_context_mut();
140+
this.set_last_error(err)?;
141+
Ok(Scalar::from_i32(-1))
142+
}
143+
122144
/// Gets the last error variable.
123145
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar> {
124146
let this = self.eval_context_mut();
@@ -185,18 +207,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
185207
}
186208
}
187209

188-
/// Sets the last OS error using a `std::io::ErrorKind` and writes -1 to dest place.
189-
fn set_last_error_and_return(
190-
&mut self,
191-
err: impl Into<IoError>,
192-
dest: &MPlaceTy<'tcx>,
193-
) -> InterpResult<'tcx> {
194-
let this = self.eval_context_mut();
195-
this.set_last_error(err)?;
196-
this.write_int(-1, dest)?;
197-
Ok(())
198-
}
199-
200210
/// Helper function that consumes an `std::io::Result<T>` and returns an
201211
/// `InterpResult<'tcx,T>::Ok` instead. In case the result is an error, this function returns
202212
/// `Ok(-1)` and sets the last OS error accordingly.

src/shims/unix/env.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
177177
Ok(Scalar::from_i32(0)) // return zero on success
178178
} else {
179179
// name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
180-
this.set_last_error(LibcError("EINVAL"))?;
181-
Ok(Scalar::from_i32(-1))
180+
this.set_last_error_and_return_i32(LibcError("EINVAL"))
182181
}
183182
}
184183

@@ -202,8 +201,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
202201
Ok(Scalar::from_i32(0))
203202
} else {
204203
// name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
205-
this.set_last_error(LibcError("EINVAL"))?;
206-
Ok(Scalar::from_i32(-1))
204+
this.set_last_error_and_return_i32(LibcError("EINVAL"))
207205
}
208206
}
209207

@@ -242,9 +240,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
242240

243241
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
244242
this.reject_in_isolation("`chdir`", reject_with)?;
245-
this.set_last_error(ErrorKind::PermissionDenied)?;
246-
247-
return Ok(Scalar::from_i32(-1));
243+
return this.set_last_error_and_return_i32(ErrorKind::PermissionDenied);
248244
}
249245

250246
let result = env::set_current_dir(path).map(|()| 0);

src/shims/unix/fd.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
524524
// Reject if isolation is enabled.
525525
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
526526
this.reject_in_isolation("`fcntl`", reject_with)?;
527-
this.set_last_error(ErrorKind::PermissionDenied)?;
528-
return Ok(Scalar::from_i32(-1));
527+
return this.set_last_error_and_return_i32(ErrorKind::PermissionDenied);
529528
}
530529

531530
this.ffullsync_fd(fd_num)
@@ -606,9 +605,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
606605
None => fd.read(&fd, communicate, buf, count, dest, this)?,
607606
Some(offset) => {
608607
let Ok(offset) = u64::try_from(offset) else {
609-
this.set_last_error(LibcError("EINVAL"))?;
610-
this.write_int(-1, dest)?;
611-
return Ok(());
608+
return this.set_last_error_and_return(LibcError("EINVAL"), dest);
612609
};
613610
fd.pread(communicate, offset, buf, count, dest, this)?
614611
}
@@ -650,9 +647,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
650647
None => fd.write(&fd, communicate, buf, count, dest, this)?,
651648
Some(offset) => {
652649
let Ok(offset) = u64::try_from(offset) else {
653-
this.set_last_error(LibcError("EINVAL"))?;
654-
this.write_int(-1, dest)?;
655-
return Ok(());
650+
return this.set_last_error_and_return(LibcError("EINVAL"), dest);
656651
};
657652
fd.pwrite(communicate, buf, count, offset, dest, this)?
658653
}

0 commit comments

Comments
 (0)