Skip to content

Commit d24a2ef

Browse files
committed
test: add system_time_extrem_values_regression
1 parent 9cd272d commit d24a2ef

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

library/std/tests/time.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,28 @@ fn big_math() {
227227
check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub);
228228
check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub);
229229
}
230+
231+
#[test]
232+
fn system_time_extreme_values_regression() {
233+
// Test for regression in SystemTime comparison with extreme values
234+
// This test covers the bug introduced in PR #144519 where integer overflow
235+
// in the comparison logic caused incorrect results when dealing with times
236+
// near i64::MIN and i64::MAX.
237+
//
238+
// This is the exact test case from GitHub issue #146228
239+
let t = SystemTime::UNIX_EPOCH;
240+
let early = t - (Duration::from_secs(i64::MAX as u64 + 1));
241+
let later = t + (Duration::from_secs(i64::MAX as u64) + Duration::from_nanos(999_999_999));
242+
243+
// This should succeed and not return a SystemTimeError due to incorrect comparison overflow
244+
let delta =
245+
later.duration_since(early).expect("duration_since should work with extreme values");
246+
247+
// Verify that the delta calculation is reasonable
248+
// early is at approximately -i64::MAX-1 seconds from epoch
249+
// later is at approximately i64::MAX seconds + 999_999_999 nanoseconds from epoch
250+
// So delta should be approximately (i64::MAX + i64::MAX + 1) seconds + 999_999_999 nanoseconds
251+
let expected_secs = (i64::MAX as u64) * 2 + 1;
252+
let expected = Duration::new(expected_secs, 999_999_999);
253+
assert_eq!(delta, expected, "Duration calculation should be correct for extreme values");
254+
}

0 commit comments

Comments
 (0)