-
Notifications
You must be signed in to change notification settings - Fork 2.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
Fix overflow in tm formatter #2564
Conversation
0a1a08e
to
0465eca
Compare
@vitaut, review this PR please. |
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.
Thanks for the fix. Looks good overall, just a few minor comments inline.
include/fmt/chrono.h
Outdated
@@ -1406,80 +1406,112 @@ template <typename OutputIt, typename Char> class tm_writer { | |||
OutputIt out_; | |||
const std::tm& tm_; | |||
|
|||
auto tm_year() const noexcept -> int { return 1900 + tm_.tm_year; } | |||
auto tm_sec() const noexcept -> int { | |||
FMT_ASSERT(tm_.tm_sec >= 0 && tm_.tm_sec <= 60, ""); |
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.
According to https://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html tm_sec should be in the range [0, 61]
so the upper bound should be 61 here.
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.
It's [0..60] since C99 and C++11.
https://en.cppreference.com/w/c/chrono/tm
https://en.cppreference.com/w/cpp/chrono/c/tm
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.
For compatibility, I changed the interval to [0, 61]
.
return tm_.tm_yday; | ||
} | ||
|
||
auto tm_hour12() const noexcept -> int { |
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.
I suggest merging this function into on_12_hour
since it's not used anywhere else.
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 function tm_hour12
will be used in on_12_hour_time
. PR #2550 (https://github.com/fmtlib/fmt/pull/2550/files#diff-5d08eff445a9a7793fbd22d873b2284f0ae69f1550aff06f673de49768bccca0R1744-R1750)
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.
OK, let's keep it then.
Switch internal year calculations to long long
0465eca
to
75f7dd2
Compare
@vitaut
|
The documentation issue looks unrelated, merged. |
…2564) Switch internal year calculations to long long
Fix possible overflow errors in
tm
formatter on invalidtm
values.