std: respect the bounds of the platform's data structures when performing time arithmetic #147072
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
InstantandSystemTimemention thatand
In my understanding, this implies that, should calling
checked_addorchecked_subon anInstant/SystemTimesucceed, 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
Durations and use thoseDurations to perform arithmetic. Thus it becomes possible to createInstants/SystemTimes that are not representable by the OS, which becomes problematic when considering functions such asFile::set_timesorsleep_until.This PR thus changes the
InstantandSystemTimestructures 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::elapsedwill now only do one conversion instead of two)I've left UEFI's
Instantuntouched 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_timescan fail because of an invalidSystemTimeis on 32-bit UNIXes that lack 64-bit time support. Changing them would conflict with the guarantee that 100 year additions are portable, however.