-
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
Conversation
rustbot has assigned @workingjubilee. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Note that the same issue is present in several implementations, so, you're going to want to change all of them. But also, you could just use saturating_sub instead of extra branches to avoid changing too much here. |
785a624
to
ad658e7
Compare
This comment has been minimized.
This comment has been minimized.
ad658e7
to
dfffc94
Compare
This comment has been minimized.
This comment has been minimized.
dfffc94
to
381f3e3
Compare
This comment has been minimized.
This comment has been minimized.
ffcafa6
to
3926ef1
Compare
yes, there also a same issue in
Thank you. |
3926ef1
to
8b81b62
Compare
This comment has been minimized.
This comment has been minimized.
Why it failed, I execute |
It is supposed to panic, no? You changed
to
to avoid overflow, but code is panicking on exactly the same subtraction:
I think |
Also the test you added, it is Linux/mac specific, won't work on Window, because |
Or, |
8b81b62
to
d3a47fb
Compare
What is the easiest way to check all these platforms? At least check they compile? |
I included a list of the targets I tested in the PR, but the answer is it's a bit of a mess. You need a few different cross-compilers to get it working. The merge job for Bors will run all them regardless, though, so worst case you can do a try job before adding to the queue to verify. You might get lucky with only needing the x86_64 ones, though, in which case it'll be much easier. And, since you're just modifying places where the code was copied, it's less likely to break than figuring out what const markers are needed. |
57cb25a
to
1139c62
Compare
638eebf
to
7c0dd68
Compare
} else { | ||
Duration::new( | ||
(self.t.tv_sec - 1 - other.t.tv_sec) as u64, | ||
self.ttv_sec.wrapping_sub(other.t.tv_sec) as u64 - 1_u64, |
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.
ttv_sec
Typo here.
Signed-off-by: Eval EXEC <[email protected]>
7c0dd68
to
d2bc288
Compare
SystemTime::duration_since
panic for extreme value
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.
This has had a number of changes since it was first opened. Can you explain why the current approach? I know an issue was opened but it is not guaranteed that we should simply fix it, or at least it is not guaranteed how exactly we should, especially given the "that is allowed to break, actually" quality of some of the operations.
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)); |
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.
Offset of the SystemTime by a Duration is not guaranteed to give a sensible result and I do not believe cfg(unix)
is sufficient for this.
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.
In current std implementation, this operation supposed to succeed, because, SystemTime
is defined using Timespec
that has i64
seconds part:
rust/library/std/src/sys/pal/mod.rs
Lines 27 to 31 in 7ad23f4
cfg_select! { | |
unix => { | |
mod unix; | |
pub use self::unix::*; | |
} |
rust/library/std/src/sys/pal/unix/time.rs
Lines 20 to 29 in 7ad23f4
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
pub struct SystemTime { | |
pub(crate) t: Timespec, | |
} | |
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
pub(crate) struct Timespec { | |
tv_sec: i64, | |
tv_nsec: Nanoseconds, | |
} |
Rust std may want to reduce the range of SystemTime
or Timespec
one day, but I don't think this is likely, and if that happens, the test can be changed (and likely other tests too).
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.
Hmm. My concern was that I am not entirely sure everything that uses cfg(unix)
uses that file, but I can allow such problems to be handled separately, so I suppose I will.
I don't this this statement is true in regards to
|
It is panic only if std is compiled with debug assertions; for regular user of std, nightly behavior is incorrectly returned error, but not panic: play. Previous title may have been more correct. |
SystemTime::duration_since
panic for extreme valueSystemTime::duration_since
error for extreme value
I see. Thank you for elaborating. I wish we converted to some internal representation of a datetime that made sense relative to Duration and then created the Duration from that... |
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.
@eval-exec Can you take what stepancheg just said and paraphrase it as the PR description? Or provide your own words. Just make it a little clearer on reading the description what is being fixed and why it must be fixed, then I can r+ this.
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)); |
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.
Hmm. My concern was that I am not entirely sure everything that uses cfg(unix)
uses that file, but I can allow such problems to be handled separately, so I suppose I will.
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:
- SOLID's changes were purely organizational
- SGX's changed to explicit match because
or_else
isn't constifiable - "unsupported" removed
or_else
- WASI removed
or_else
andtry_into
- Windows removed
try_into
So they had no notable logic changes.
Ah, and since this is waiting for that PR description update: @rustbot author |
Reminder, once the PR becomes ready for a review, use |
IMO the better fix is to revert the code changes from #144519, and wait until the original code can be constified unchanged. That PR went too far with introducing const-hacks for not enough gain. We're in no rush to make Also see Zulip discussion. |
I think that it's reasonable to just revert the change at least until PartialOrd is made unstably const (this is the only thing that's const-hacked, at the moment.) I also have a particular… issue, with the way the time system code is defined. A lot of it is copy-pasted which makes issues harder to test outside of extremely niche targets, when the code itself is target-independent. |
Revert is up at #146473. |
☔ The latest upstream changes (presumably #146526) made this pull request unmergeable. Please resolve the merge conflicts. |
Superseded by #146473. |
This PR intends to fix #146228