File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed
library/std/src/sys/pal/unix Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -139,10 +139,13 @@ impl Timespec {
139
139
#[ rustc_const_unstable( feature = "const_system_time" , issue = "144517" ) ]
140
140
pub const fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
141
141
// FIXME: const PartialOrd
142
- let mut cmp = self . tv_sec - other. tv_sec ;
143
- if cmp == 0 {
144
- cmp = self . tv_nsec . as_inner ( ) as i64 - other. tv_nsec . as_inner ( ) as i64 ;
145
- }
142
+ // Use saturating arithmetic to avoid overflow when comparing extreme values
143
+ let mut cmp = self . tv_sec . saturating_sub ( other. tv_sec ) ;
144
+ cmp = if cmp == 0 {
145
+ self . tv_nsec . as_inner ( ) as i64 - other. tv_nsec . as_inner ( ) as i64
146
+ } else {
147
+ cmp
148
+ } ;
146
149
147
150
if cmp >= 0 {
148
151
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
You can’t perform that action at this time.
0 commit comments