From 1f48f8f6e289d3ae14d28ad9bd000ef5ba209fc0 Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Tue, 9 Jun 2020 12:31:12 -0400 Subject: [PATCH] [libc++] Avoid UB in year_month_day_last::day() for incorrect months 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcxx/include/chrono b/libcxx/include/chrono index 6e5de39..117aab3 100644 --- a/libcxx/include/chrono +++ b/libcxx/include/chrono @@ -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(month()) - 1] : chrono::day{29}; } -- 2.7.4