[libcxx-commits] [PATCH] D81477: [libc++] Avoid UB in year_month_day_last::day() for incorrect months

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 9 09:52:28 PDT 2020

ldionne created this revision.
ldionne added a reviewer: howard.hinnant.
Herald added subscribers: libcxx-commits, dexonsmith, jkorous.
Herald added a project: libc++.
Herald added a reviewer: libc++.
ldionne added a parent revision: D70346: [libc++] [LWG3231] Mark "year_month_day_last::day() specification does not cover !ok() values" issue as "Nothing to do", but add assertion..
ldionne added a comment.

Note that a test was added in cb347a1106a76e248a6c0d78451a018b20662c03 <https://reviews.llvm.org/rGcb347a1106a76e248a6c0d78451a018b20662c03>, and in fact this UB did trigger a failure in the ASAN bot due to the added test.

So basically this commit fixes the LWG issue, and the tests for it have been added in cb347a1106a76e248a6c0d78451a018b20662c03 <https://reviews.llvm.org/rGcb347a1106a76e248a6c0d78451a018b20662c03>. Sorry if that's confusing, everything should have been added in this commit, but I didn't fully understand the issue yet and I didn't know this commit was going to be necessary.

This effectively implements the resolution of LWG3231, which mandates
that calling year_month_day_last::day() on an invalid year_month_day_last
is unspecified behavior. Before this change, it was undefined behavior.

  rG LLVM Github Monorepo



Index: libcxx/include/chrono
--- libcxx/include/chrono
+++ libcxx/include/chrono
@@ -2454,7 +2454,7 @@
         chrono::day(31), chrono::day(31), chrono::day(30),
         chrono::day(31), chrono::day(30), chrono::day(31)
-    return month() != February || !__y.is_leap() ?
+    return (month() != February || !__y.is_leap()) && month().ok() ?
         __d[static_cast<unsigned>(month()) - 1] : chrono::day{29};

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81477.269570.patch
Type: text/x-patch
Size: 493 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200609/93d58abf/attachment.bin>

More information about the libcxx-commits mailing list