Skip to content

Commit d2bc288

Browse files
committed
library: sub_timespec use arithmetic to avoid overflow
Signed-off-by: Eval EXEC <[email protected]>
1 parent b9e70ca commit d2bc288

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

library/std/src/sys/pal/hermit/time.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ impl Timespec {
2828
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
2929
const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
3030
// FIXME: const PartialOrd
31-
let mut cmp = self.t.tv_sec - other.t.tv_sec;
31+
let mut cmp = self.t.tv_sec.saturating_sub(other.t.tv_sec);
3232
if cmp == 0 {
3333
cmp = self.t.tv_nsec as i64 - other.t.tv_nsec as i64;
3434
}
3535

3636
if cmp >= 0 {
3737
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
3838
Duration::new(
39-
(self.t.tv_sec - other.t.tv_sec) as u64,
39+
self.t.tv_sec.wrapping_sub(other.t.tv_sec) as u64,
4040
(self.t.tv_nsec - other.t.tv_nsec) as u32,
4141
)
4242
} else {
4343
Duration::new(
44-
(self.t.tv_sec - 1 - other.t.tv_sec) as u64,
44+
self.t.tv_sec.wrapping_sub(other.t.tv_sec) as u64 - 1_u64,
4545
(self.t.tv_nsec + NSEC_PER_SEC - other.t.tv_nsec) as u32,
4646
)
4747
})

library/std/src/sys/pal/uefi/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl SystemTime {
9595

9696
// Check if can be represented in UEFI
9797
// FIXME: const PartialOrd
98-
let mut cmp = temp.as_secs() - MAX_UEFI_TIME.0.as_secs();
98+
let mut cmp = temp.as_secs().saturating_sub(MAX_UEFI_TIME.0.as_secs());
9999
if cmp == 0 {
100100
cmp = temp.subsec_nanos() as u64 - MAX_UEFI_TIME.0.subsec_nanos() as u64;
101101
}

library/std/src/sys/pal/unix/time.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Timespec {
139139
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
140140
pub const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
141141
// FIXME: const PartialOrd
142-
let mut cmp = self.tv_sec - other.tv_sec;
142+
let mut cmp = self.tv_sec.saturating_sub(other.tv_sec);
143143
if cmp == 0 {
144144
cmp = self.tv_nsec.as_inner() as i64 - other.tv_nsec.as_inner() as i64;
145145
}
@@ -160,12 +160,12 @@ impl Timespec {
160160
// directly expresses the lower-cost behavior we want from it.
161161
let (secs, nsec) = if self.tv_nsec.as_inner() >= other.tv_nsec.as_inner() {
162162
(
163-
(self.tv_sec - other.tv_sec) as u64,
163+
self.tv_sec.wrapping_sub(other.tv_sec) as u64,
164164
self.tv_nsec.as_inner() - other.tv_nsec.as_inner(),
165165
)
166166
} else {
167167
(
168-
(self.tv_sec - other.tv_sec - 1) as u64,
168+
self.tv_sec.wrapping_sub(other.tv_sec) as u64 - 1_u64,
169169
self.tv_nsec.as_inner() + (NSEC_PER_SEC as u32) - other.tv_nsec.as_inner(),
170170
)
171171
};

0 commit comments

Comments
 (0)