std: respect the bounds of the platform's data structures when performing time arithmetic #147072
+220
−176
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The docs for
Instant
andSystemTime
mention thatand
In my understanding, this implies that, should calling
checked_add
orchecked_sub
on anInstant
/SystemTime
succeed, it should always be possible to pass the resulting time value back to the operating system.This expectation is currently violated on Windows and a number of other non-UNIX platforms (SGX, VEX, WASIp1, WASIp2, Xous) that immediately convert time values retrieved from the OS into
Duration
s and use thoseDuration
s to perform arithmetic. Thus it becomes possible to createInstant
s/SystemTime
s that are not representable by the OS, which becomes problematic when considering functions such asFile::set_times
orsleep_until
.This PR thus changes the
Instant
andSystemTime
structures on the aforementioned platforms to store the data structure returned by their OS and changes the arithmetic functions to act on those values. In particular, this results.checked_add(Duration::from_nanos(1))
might return the same time value)Instant
(this might actually be beneficial asInstant::elapsed
will now only do one conversion instead of two)I've left UEFI's
Instant
untouched in this PR, even though it is suffering from the same issue, as the fix is quite non-trivial there.After this PR, the only platform where
File::set_times
can fail because of an invalidSystemTime
is on 32-bit UNIXes that lack 64-bit time support. Changing them would conflict with the guarantee that 100 year additions are portable, however.