-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
<chrono>
: integer overflow in weekday::weekday(sys_days::max())
#5153
Comments
Perhaps Curiously, when I attempted to simplify the suggestion to just returning |
And here I am being let down that the optimizer doesn't know that |
As @CaseyCarter pointed out, it's sad that the compiler cannot figure out that
By knowing that Having said that, I don't mind if the I'm sorry I couldn't follow the "normal" process (i.e., checkout, change, add a test, build, run and submit a PR). It would take me long time to learn how to do all that, whereas for a regular STL developer, putting this fix and add a test wouldn't take that much. (FWIW: I did do all that for libstdc++.) |
Perhaps the discussion on
Given the constructors are implemented as below currently: Lines 433 to 438 in 5e0ddad
The 2 constructors below are the only callers of However, expressing postconditions may be helpful for callers of
I'm now creating a PR and listing you as the co-author. Please double-check it. |
@frederick-vs-ja Oh! You're completely right regarding I've made a suggestion in your PR, just in case you wish to support other platforms. Otherwise, the code looks good to me. Thanks for fixing. |
<chrono>
: integer overflow in weekday::weekday(sys_days::max())
.
<chrono>
: integer overflow in weekday::weekday(sys_days::max())
.<chrono>
: integer overflow in weekday::weekday(sys_days::max())
Brief description
The following code fails to compile due to an
int
addition overflow:https://godbolt.org/z/K6dW7bvYq
Details
The overflow occurs in the evaluation of
_Tp + 4
belowIndeed, this addition overflows whenever
_Tp > INT_MAX - 4
.Fixes
Provided that the range of
long long int
is larger than that ofint
, the obvious fix is declaring_Tp
aslong long int
and an alternative is this:This alternative is better than the obvious fix and better than the current implementation. Indeed, with this fix the codegen of
weekday::weekday(const sys_days&)
becomes branch-free and avoidsdiv
instructions on x64, x86 and arm64.Test
The alternative above works as an exhaustive test shows:
(where
weekday_ctr
is an impersonation ofweekday::weekday(const sys_days&)
using the fixed_Weekday_from_days
.)The text was updated successfully, but these errors were encountered: