[libc++] Avoid UB in year_month_day_last::day() for incorrect months
authorLouis Dionne <ldionne@apple.com>
Tue, 9 Jun 2020 16:31:12 +0000 (12:31 -0400)
committerLouis Dionne <ldionne@apple.com>
Tue, 9 Jun 2020 17:43:13 +0000 (13:43 -0400)
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.

Differential Revision: https://reviews.llvm.org/D81477

libcxx/include/chrono

index 6e5de39..117aab3 100644 (file)
@@ -2454,7 +2454,7 @@ chrono::day year_month_day_last::day() const noexcept
         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};
 }