Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 79c9c80

Browse files
committed
Use strict ops instead of checked ops
1 parent 66ad792 commit 79c9c80

File tree

14 files changed

+58
-68
lines changed

14 files changed

+58
-68
lines changed

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl GlobalStateInner {
9797
fn align_addr(addr: u64, align: u64) -> u64 {
9898
match addr % align {
9999
0 => addr,
100-
rem => addr.checked_add(align).unwrap() - rem,
100+
rem => addr.strict_add(align) - rem,
101101
}
102102
}
103103

src/tools/miri/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ pub fn create_ecx<'tcx>(
303303
let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
304304
for arg in config.args.iter() {
305305
// Make space for `0` terminator.
306-
let size = u64::try_from(arg.len()).unwrap().checked_add(1).unwrap();
306+
let size = u64::try_from(arg.len()).unwrap().strict_add(1);
307307
let arg_type = Ty::new_array(tcx, tcx.types.u8, size);
308308
let arg_place =
309309
ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Machine.into())?;

src/tools/miri/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
963963
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
964964
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
965965
let string_length = u64::try_from(c_str.len()).unwrap();
966-
let string_length = string_length.checked_add(1).unwrap();
966+
let string_length = string_length.strict_add(1);
967967
if size < string_length {
968968
return Ok((false, string_length));
969969
}
@@ -1027,7 +1027,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10271027
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
10281028
// 0x0000 terminator to memory would cause an out-of-bounds access.
10291029
let string_length = u64::try_from(wide_str.len()).unwrap();
1030-
let string_length = string_length.checked_add(1).unwrap();
1030+
let string_length = string_length.strict_add(1);
10311031
if size < string_length {
10321032
return Ok((false, string_length));
10331033
}
@@ -1391,7 +1391,7 @@ pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
13911391
if success {
13921392
// If the function succeeds, the return value is the number of characters stored in the target buffer,
13931393
// not including the terminating null character.
1394-
u32::try_from(len.checked_sub(1).unwrap()).unwrap()
1394+
u32::try_from(len.strict_sub(1)).unwrap()
13951395
} else {
13961396
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
13971397
// required to hold the string and its terminating null character.

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
402402
});
403403
let (_, addr) = ptr.into_parts(); // we know the offset is absolute
404404
// Cannot panic since `align` is a power of 2 and hence non-zero.
405-
if addr.bytes().checked_rem(align.bytes()).unwrap() != 0 {
405+
if addr.bytes().strict_rem(align.bytes()) != 0 {
406406
throw_unsup_format!(
407407
"`miri_promise_symbolic_alignment`: pointer is not actually aligned"
408408
);
@@ -714,7 +714,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
714714
// That is probably overly cautious, but there also is no fundamental
715715
// reason to have `strcpy` destroy pointer provenance.
716716
// This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
717-
let n = this.read_c_str(ptr_src)?.len().checked_add(1).unwrap();
717+
let n = this.read_c_str(ptr_src)?.len().strict_add(1);
718718
this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
719719
this.write_pointer(ptr_dest, dest)?;
720720
}

src/tools/miri/src/shims/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
165165
("tm_hour", dt.hour().into()),
166166
("tm_mday", dt.day().into()),
167167
("tm_mon", dt.month0().into()),
168-
("tm_year", dt.year().checked_sub(1900).unwrap().into()),
168+
("tm_year", dt.year().strict_sub(1900).into()),
169169
("tm_wday", dt.weekday().num_days_from_sunday().into()),
170170
("tm_yday", dt.ordinal0().into()),
171171
("tm_isdst", tm_isdst),

src/tools/miri/src/shims/unix/env.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ impl<'tcx> UnixEnvVars<'tcx> {
8181
return Ok(None);
8282
};
8383
// The offset is used to strip the "{name}=" part of the string.
84-
let var_ptr = var_ptr.offset(
85-
Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
86-
ecx,
87-
)?;
84+
let var_ptr = var_ptr
85+
.offset(Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)), ecx)?;
8886
Ok(Some(var_ptr))
8987
}
9088

src/tools/miri/src/shims/unix/fd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl FdTable {
240240
let new_fd = candidate_new_fd.unwrap_or_else(|| {
241241
// find_map ran out of BTreeMap entries before finding a free fd, use one plus the
242242
// maximum fd in the map
243-
self.fds.last_key_value().map(|(fd, _)| fd.checked_add(1).unwrap()).unwrap_or(min_fd)
243+
self.fds.last_key_value().map(|(fd, _)| fd.strict_add(1)).unwrap_or(min_fd)
244244
});
245245

246246
self.fds.try_insert(new_fd, file_handle).unwrap();

src/tools/miri/src/shims/unix/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl FileDescription for SocketPair {
116116
};
117117
let mut writebuf = writebuf.borrow_mut();
118118
let data_size = writebuf.buf.len();
119-
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.checked_sub(data_size).unwrap();
119+
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.strict_sub(data_size);
120120
if available_space == 0 {
121121
if self.is_nonblock {
122122
// Non-blocking socketpair with a full buffer.

src/tools/miri/src/shims/windows/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
647647
// If the function succeeds, the return value is the length of the string that
648648
// is copied to the buffer, in characters, not including the terminating null
649649
// character.
650-
this.write_int(size_needed.checked_sub(1).unwrap(), dest)?;
650+
this.write_int(size_needed.strict_sub(1), dest)?;
651651
} else {
652652
// If the buffer is too small to hold the module name, the string is truncated
653653
// to nSize characters including the terminating null character, the function
@@ -689,7 +689,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
689689
throw_unsup_format!("FormatMessageW: buffer not big enough");
690690
}
691691
// The return value is the number of characters stored *excluding* the null terminator.
692-
this.write_int(length.checked_sub(1).unwrap(), dest)?;
692+
this.write_int(length.strict_sub(1), dest)?;
693693
}
694694

695695
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.

src/tools/miri/src/shims/windows/handle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Handle {
7474
/// None of this layout is guaranteed to applications by Windows or Miri.
7575
fn to_packed(self) -> u32 {
7676
let disc_size = Self::packed_disc_size();
77-
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
77+
let data_size = u32::BITS.strict_sub(disc_size);
7878

7979
let discriminant = self.discriminant();
8080
let data = self.data();
@@ -103,7 +103,7 @@ impl Handle {
103103
/// see docs for `to_packed`
104104
fn from_packed(handle: u32) -> Option<Self> {
105105
let disc_size = Self::packed_disc_size();
106-
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
106+
let data_size = u32::BITS.strict_sub(disc_size);
107107

108108
// the lower `data_size` bits of this mask are 1
109109
#[allow(clippy::arithmetic_side_effects)] // cannot overflow

0 commit comments

Comments
 (0)