Skip to content

Commit d91b733

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

File tree

1 file changed

+3
-2
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+3
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ 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+
// Use saturating arithmetic to avoid overflow when comparing extreme values
143+
let mut cmp = self.tv_sec.saturating_sub(other.tv_sec);
143144
if cmp == 0 {
144-
cmp = self.tv_nsec.as_inner() as i64 - other.tv_nsec.as_inner() as i64;
145+
cmp = self.tv_nsec.as_inner() as i64 - other.tv_nsec.as_inner() as i64
145146
}
146147

147148
if cmp >= 0 {

0 commit comments

Comments
 (0)