Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions library/std/src/sys/pal/hermit/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ impl Timespec {
}

fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
fn sub_ge_to_unsigned(a: i64, b: i64) -> u64 {
debug_assert!(a >= b);
a.wrapping_sub(b).cast_unsigned()
}

if self >= other {
// Logic here is identical to Unix version of `Timestamp::sub_timespec`,
// check comments there why operations do not overflow.
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
Duration::new(
(self.t.tv_sec - other.t.tv_sec) as u64,
sub_ge_to_unsigned(self.t.tv_sec, other.t.tv_sec),
(self.t.tv_nsec - other.t.tv_nsec) as u32,
)
} else {
Duration::new(
(self.t.tv_sec - 1 - other.t.tv_sec) as u64,
sub_ge_to_unsigned(self.t.tv_sec - 1, other.t.tv_sec),
(self.t.tv_nsec + NSEC_PER_SEC - other.t.tv_nsec) as u32,
)
})
Expand Down
27 changes: 12 additions & 15 deletions library/std/src/sys/pal/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,25 @@ impl Timespec {
}

pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
// When a >= b, the difference fits in u64.
fn sub_ge_to_unsigned(a: i64, b: i64) -> u64 {
debug_assert!(a >= b);
a.wrapping_sub(b).cast_unsigned()
}

if self >= other {
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
// to optimize it into a branchless form (see also #75545):
//
// 1. `self.tv_sec - other.tv_sec` shows up as a common expression
// in both branches, i.e. the `else` must have its `- 1`
// subtraction after the common one, not interleaved with it
// (it used to be `self.tv_sec - 1 - other.tv_sec`)
//
// 2. the `Duration::new` call (or any other additional complexity)
// is outside of the `if`-`else`, not duplicated in both branches
//
// Ideally this code could be rearranged such that it more
// directly expresses the lower-cost behavior we want from it.
let (secs, nsec) = if self.tv_nsec.as_inner() >= other.tv_nsec.as_inner() {
(
(self.tv_sec - other.tv_sec) as u64,
sub_ge_to_unsigned(self.tv_sec, other.tv_sec),
self.tv_nsec.as_inner() - other.tv_nsec.as_inner(),
)
} else {
// Following sequence of assertions explain why `self.tv_sec - 1` does not underflow.
debug_assert!(self.tv_nsec < other.tv_nsec);
debug_assert!(self.tv_sec > other.tv_sec);
debug_assert!(self.tv_sec > i64::MIN);
(
(self.tv_sec - other.tv_sec - 1) as u64,
sub_ge_to_unsigned(self.tv_sec - 1, other.tv_sec),
self.tv_nsec.as_inner() + (NSEC_PER_SEC as u32) - other.tv_nsec.as_inner(),
)
};
Expand Down
16 changes: 16 additions & 0 deletions library/std/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,19 @@ fn big_math() {
check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub);
check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub);
}

#[test]
#[cfg(unix)]
fn system_time_duration_since_max_range_on_unix() {
// Repro regression https://github.com/rust-lang/rust/issues/146228

// Min and max values of `SystemTime` on Unix.
let min = SystemTime::UNIX_EPOCH - (Duration::new(i64::MAX as u64 + 1, 0));
let max = SystemTime::UNIX_EPOCH + (Duration::new(i64::MAX as u64, 999_999_999));

let delta_a = max.duration_since(min).expect("duration_since overflow");
let delta_b = min.duration_since(max).expect_err("duration_since overflow").duration();

assert_eq!(Duration::MAX, delta_a);
assert_eq!(Duration::MAX, delta_b);
}
Loading