Skip to content

Commit

Permalink
ICU-22962 Fix int overflow in Calendar::handleComputeJulianDay
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Dec 21, 2024
1 parent 7d60bb8 commit 4c9ef1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion icu4c/source/i18n/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3607,7 +3607,12 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField, UErrorCo
fprintf(stderr, "%s:%d - y=%d, y-1=%d doy%d, njd%d (C.F. %d)\n",
__FILE__, __LINE__, year, year-1, testDate, julianDay+testDate, nextJulianDay);
#endif
if(julianDay+testDate > nextJulianDay) { // is it past Dec 31? (nextJulianDay is day BEFORE year+1's Jan 1)
if (uprv_add32_overflow(julianDay, testDate, &testDate)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}

if(testDate > nextJulianDay) { // is it past Dec 31? (nextJulianDay is day BEFORE year+1's Jan 1)
// Fire up the calculating engines.. retry YWOY = (year-1)
int32_t prevYear;
if (uprv_add32_overflow(year, -1, &prevYear)) {
Expand Down
14 changes: 14 additions & 0 deletions icu4c/source/test/intltest/caltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name,

TESTCASE_AUTO(Test22633ChineseOverflow);
TESTCASE_AUTO(Test22962ChineseOverflow);
TESTCASE_AUTO(Test22962BuddhistOverflow);
TESTCASE_AUTO(Test22633IndianOverflow);
TESTCASE_AUTO(Test22633IslamicUmalquraOverflow);
TESTCASE_AUTO(Test22633PersianOverflow);
Expand Down Expand Up @@ -5665,6 +5666,19 @@ void CalendarTest::Test22633ChineseOverflow() {
assertTrue("Should return falure", U_FAILURE(status));
}

void CalendarTest::Test22962BuddhistOverflow() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<Calendar> cal(Calendar::createInstance(Locale("en@calendar=uddhist"), status), status);
U_ASSERT(U_SUCCESS(status));
cal->clear();
cal->set(UCAL_WEEK_OF_YEAR, 1666136);
cal->set(UCAL_YEAR, -1887379272);
cal->fieldDifference(
261830011167902373443927125260580558779842815957727840993886210772873194951140935848493861585917165011373697198856398176256.000000,
UCAL_YEAR_WOY, status);
assertTrue("Should return falure", U_FAILURE(status));
}

void CalendarTest::Test22962ChineseOverflow() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<Calendar> cal(Calendar::createInstance(Locale("en@calendar=chinese"), status), status);
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/caltest.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class CalendarTest: public CalendarTimeZoneTest {
void TestRollWeekOfYear();
void Test22633ChineseOverflow();
void Test22962ChineseOverflow();
void Test22962BuddhistOverflow();
void Test22633IndianOverflow();
void Test22633IslamicUmalquraOverflow();
void Test22633PersianOverflow();
Expand Down

0 comments on commit 4c9ef1a

Please sign in to comment.