-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Fix SystemTime::duration_since
error for extreme value
#146247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -227,3 +227,29 @@ 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_extreme_values_regression() { | ||||||||||||||||||||||||||||||||
eval-exec marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
// Test for regression in SystemTime comparison with extreme values | ||||||||||||||||||||||||||||||||
// This test covers the bug introduced in PR #144519 where integer overflow | ||||||||||||||||||||||||||||||||
// in the comparison logic caused incorrect results when dealing with times | ||||||||||||||||||||||||||||||||
// near i64::MIN and i64::MAX. | ||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||
// This is the exact test case from GitHub issue #146228 | ||||||||||||||||||||||||||||||||
let t = SystemTime::UNIX_EPOCH; | ||||||||||||||||||||||||||||||||
let early = t - (Duration::from_secs(i64::MAX as u64 + 1)); | ||||||||||||||||||||||||||||||||
let later = t + (Duration::from_secs(i64::MAX as u64) + Duration::from_nanos(999_999_999)); | ||||||||||||||||||||||||||||||||
Comment on lines
+240
to
+242
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Offset of the SystemTime by a Duration is not guaranteed to give a sensible result and I do not believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In current std implementation, this operation supposed to succeed, because, rust/library/std/src/sys/pal/mod.rs Lines 27 to 31 in 7ad23f4
rust/library/std/src/sys/pal/unix/time.rs Lines 20 to 29 in 7ad23f4
Rust std may want to reduce the range of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. My concern was that I am not entirely sure everything that uses |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// This should succeed and not return a SystemTimeError due to incorrect comparison overflow | ||||||||||||||||||||||||||||||||
let delta = | ||||||||||||||||||||||||||||||||
later.duration_since(early).expect("duration_since should work with extreme values"); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
// Verify that the delta calculation is reasonable | ||||||||||||||||||||||||||||||||
// early is at approximately -i64::MAX-1 seconds from epoch | ||||||||||||||||||||||||||||||||
// later is at approximately i64::MAX seconds + 999_999_999 nanoseconds from epoch | ||||||||||||||||||||||||||||||||
// So delta should be approximately (i64::MAX + i64::MAX + 1) seconds + 999_999_999 nanoseconds | ||||||||||||||||||||||||||||||||
let expected_secs = (i64::MAX as u64) * 2 + 1; | ||||||||||||||||||||||||||||||||
let expected = Duration::new(expected_secs, 999_999_999); | ||||||||||||||||||||||||||||||||
assert_eq!(delta, expected, "Duration calculation should be correct for extreme values"); | ||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting to myself:
or_else
isn't constifiableor_else
or_else
andtry_into
try_into
So they had no notable logic changes.