Skip to content
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

Merged
merged 1 commit into from
Oct 27, 2021

Conversation

phprus
Copy link
Contributor

@phprus phprus commented Oct 24, 2021

Fix possible overflow errors in tm formatter on invalid tm values.

@phprus phprus force-pushed the optimize-tm-formatting-3-ub-2 branch 3 times, most recently from 0a1a08e to 0465eca Compare October 25, 2021 15:10
@phprus
Copy link
Contributor Author

phprus commented Oct 26, 2021

@vitaut, review this PR please.
This is fixing year overflow and possible overflow on all digits2 calls (is tm is invalid).

Copy link
Contributor

@vitaut vitaut left a 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.

@@ -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, "");
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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].

include/fmt/chrono.h Outdated Show resolved Hide resolved
return tm_.tm_yday;
}

auto tm_hour12() const noexcept -> int {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

include/fmt/chrono.h Show resolved Hide resolved
Switch internal year calculations to long long
@phprus phprus force-pushed the optimize-tm-formatting-3-ub-2 branch from 0465eca to 75f7dd2 Compare October 27, 2021 20:14
@phprus
Copy link
Contributor Author

phprus commented Oct 27, 2021

@vitaut
Something broke on documentation build...
https://github.com/fmtlib/fmt/runs/4027135638?check_suite_focus=true

Exception occurred:
  File "/home/runner/work/fmt/build/virtualenv/lib/python3.8/site-packages/docutils/writers/html5_polyglot/__init__.py", line 445, in section_title_tags
    if (ids and self.settings.section_self_link
AttributeError: 'Values' object has no attribute 'section_self_link'

@vitaut vitaut merged commit 90034e4 into fmtlib:master Oct 27, 2021
@vitaut
Copy link
Contributor

vitaut commented Oct 27, 2021

The documentation issue looks unrelated, merged.

PoetaKodu pushed a commit to pacc-repo/fmt that referenced this pull request Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants