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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 9 10:43:45 PDT 2020


Author: Louis Dionne
Date: 2020-06-09T13:43:13-04:00
New Revision: 1f48f8f6e289d3ae14d28ad9bd000ef5ba209fc0

URL: https://github.com/llvm/llvm-project/commit/1f48f8f6e289d3ae14d28ad9bd000ef5ba209fc0
DIFF: https://github.com/llvm/llvm-project/commit/1f48f8f6e289d3ae14d28ad9bd000ef5ba209fc0.diff

LOG: [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

Added: 
    

Modified: 
    libcxx/include/chrono

Removed: 
    


################################################################################
diff  --git a/libcxx/include/chrono b/libcxx/include/chrono
index 6e5de398b72f..117aab31907f 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<unsigned>(month()) - 1] : chrono::day{29};
 }
 


        


More information about the libcxx-commits mailing list