Skip to content

Commit 8b81b62

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

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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
}

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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)