Skip to content

Commit cd757b1

Browse files
authored
Disable fsanitize=array-bounds for days_per_month (google#319)
This works around a missed optimization in clang, which does not see that m (when the whole chain is inlined into BreakTime) is always inbounds.
1 parent 306fd9b commit cd757b1

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/cctz/civil_time_detail.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ CONSTEXPR_F int days_per_4years(int yi) noexcept {
9191
CONSTEXPR_F int days_per_year(year_t y, month_t m) noexcept {
9292
return is_leap_year(y + (m > 2)) ? 366 : 365;
9393
}
94+
// The compiler cannot optimize away the check if we use
95+
// fsanitize=array-bounds.
96+
// m is guaranteed to be in [1:12] in the caller, but the compiler cannot
97+
// optimize away the check even when this function is inlined into BreakTime.
98+
// To reduce the overhead, we use no_sanitize to skip the unnecessary
99+
// fsanitize=array-bounds check. Remove no_sanitize once the missed
100+
// optimization is fixed.
101+
#if defined(__has_attribute)
102+
#if __has_attribute(no_sanitize)
103+
__attribute__((no_sanitize("array-bounds")))
104+
#endif
105+
#endif
94106
CONSTEXPR_F int days_per_month(year_t y, month_t m) noexcept {
95107
CONSTEXPR_D int k_days_per_month[1 + 12] = {
96108
-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 // non leap year

0 commit comments

Comments
 (0)