-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<chrono>: Fine grained bounds checking #1871
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>: Fine grained bounds checking #1871
Conversation
The motivation of this PR is to loosen some specifier constraints by only checking relavent portions of types instead of a blanket `ok()` before attempting to print. This should cover all the specifiers. I've also moved check-only custom prints into this new method.
stl/inc/chrono
Outdated
| if constexpr ( | ||
| _Is_specialization_v<_Ty, duration> | ||
| || is_same_v<_Ty, sys_info> || _Is_specialization_v<_Ty, time_point> | ||
| || _Is_specialization_v<_Ty, _Local_time_format_t>) { | ||
| return; | ||
| } |
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.
Clang format messes this up, but it can be formatted a little more inline (a few clauses got removed over time).
My understanding is that these types are always valid by design, so they just work.
| if constexpr (_Is_specialization_v<_Ty, hh_mm_ss>) { | ||
| if (_Spec._Type == 'H' && _Val.hours() >= hours{24}) { | ||
| _THROW(format_error("Cannot localize hh_mm_ss with an absolute value of 24 hours or more.")); | ||
| } | ||
| return; | ||
| } |
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.
Apart from H, my understanding is that hh_mm_ss is always valid (for it's specifiers).
| case 'a': | ||
| case 'A': | ||
| case 'u': | ||
| case 'w': | ||
| if constexpr (_Is_any_of_v<_Ty, weekday, weekday_last>) { | ||
| return _Val.ok(); | ||
| } else if constexpr (_Is_any_of_v<_Ty, weekday_indexed, year_month_weekday, | ||
| year_month_weekday_last>) { | ||
| return _Val.weekday().ok(); | ||
| } else if constexpr (is_same_v<_Ty, month_weekday>) { | ||
| return _Val.weekday_indexed().weekday().ok(); | ||
| } else if constexpr (is_same_v<_Ty, month_weekday_last>) { | ||
| return _Val.weekday_last().ok(); | ||
| } else if constexpr (_Is_any_of_v<_Ty, year_month_day, year_month_day_last>) { | ||
| return _Val.ok(); | ||
| } | ||
| break; |
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.
By far the finickiest specifier, because almost everything can go wrong and you can still get a weekday.
| } | ||
| return true; | ||
| } | ||
| _STL_INTERNAL_CHECK(false); |
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.
The idea is that if a specifier is being checked manually, we should check it for every type that is valid for that specifier (apart from the always correct types at the top). This way, if we miss specific handling for a type-specifier combo, we can catch it internally.
Add newline between non-chained if-statements. Clarify comments. _Validate_specifiers can be static. In test_month_day_formatter, verify that a month_day with a bogus month but valid day can't be printed as a month.
mnatsuhara
left a comment
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.
Feel free to ignore the nitpick, but the question about the year_month_day and friends' ok()ness remains...
Co-authored-by: mnatsuhara <46756417+mnatsuhara@users.noreply.github.com>
The motivation of this PR is to loosen some specifier constraints by
only checking relavent portions of types instead of a blanket
ok()before attempting to print. This should cover all the specifiers.
I've also moved check-only custom prints into this new method.