diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp index 10021e19eaa..66dd9fa2fd7 100644 --- a/icu4c/source/i18n/calendar.cpp +++ b/icu4c/source/i18n/calendar.cpp @@ -2075,7 +2075,12 @@ void Calendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& statu return; } case UCAL_JULIAN_DAY: - set(field, internalGet(field) + amount); + if (uprv_add32_overflow( + amount, internalGet(field), &amount)) { + status = U_ILLEGAL_ARGUMENT_ERROR; + return; + } + set(field, amount); return; default: // Other fields cannot be rolled by this method diff --git a/icu4c/source/test/intltest/caltest.cpp b/icu4c/source/test/intltest/caltest.cpp index 884f869bfdf..50e87c40443 100644 --- a/icu4c/source/test/intltest/caltest.cpp +++ b/icu4c/source/test/intltest/caltest.cpp @@ -195,6 +195,7 @@ void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name, TESTCASE_AUTO(Test22633IndianOverflow); TESTCASE_AUTO(Test22633IslamicUmalquraOverflow); TESTCASE_AUTO(Test22633PersianOverflow); + TESTCASE_AUTO(Test22633HebrewOverflow); TESTCASE_AUTO(Test22633AMPMOverflow); TESTCASE_AUTO(TestAddOverflow); @@ -5678,6 +5679,19 @@ void CalendarTest::Test22633PersianOverflow() { UCAL_YEAR, status); assertFalse("Should not return success", U_SUCCESS(status)); } + +void CalendarTest::Test22633HebrewOverflow() { + UErrorCode status = U_ZERO_ERROR; + LocalPointer cal(Calendar::createInstance(Locale("en@calendar=hebrew"), status), status); + U_ASSERT(U_SUCCESS(status)); + cal->clear(); + cal->roll(UCAL_JULIAN_DAY, -335544321, status); + assertTrue("Should return success", U_SUCCESS(status)); + cal->roll(UCAL_JULIAN_DAY, -1812424430, status); + assertEquals("Should return U_ILLEGAL_ARGUMENT_ERROR", + U_ILLEGAL_ARGUMENT_ERROR, status); +} + void CalendarTest::Test22633AMPMOverflow() { UErrorCode status = U_ZERO_ERROR; LocalPointer cal(Calendar::createInstance(Locale("en"), status), status); diff --git a/icu4c/source/test/intltest/caltest.h b/icu4c/source/test/intltest/caltest.h index 2e8b859a1be..f0e988501cc 100644 --- a/icu4c/source/test/intltest/caltest.h +++ b/icu4c/source/test/intltest/caltest.h @@ -338,6 +338,7 @@ public: // package void Test22633IndianOverflow(); void Test22633IslamicUmalquraOverflow(); void Test22633PersianOverflow(); + void Test22633HebrewOverflow(); void Test22633AMPMOverflow(); void verifyFirstDayOfWeek(const char* locale, UCalendarDaysOfWeek expected);