Skip to content

Commit

Permalink
ICU-22962 fix int32_t overflow inside handleComputeJulianDay
Browse files Browse the repository at this point in the history
test
  • Loading branch information
FrankYFTang committed Nov 9, 2024
1 parent fbfbe6c commit 44ea927
Show file tree
Hide file tree
Showing 3 changed files with 19 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 @@ -3609,7 +3609,12 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField, UErrorCo
#endif
if(julianDay+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)
julianDay = handleComputeMonthStart(year-1, 0, false, status); // jd before Jan 1 of previous year
int32_t prevYear;
if (uprv_add32_overflow(year, -1, &prevYear)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
julianDay = handleComputeMonthStart(prevYear, 0, false, status); // jd before Jan 1 of previous year
if (U_FAILURE(status)) {
return 0;
}
Expand Down
12 changes: 12 additions & 0 deletions icu4c/source/test/intltest/caltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
TESTCASE_AUTO(Test22633HebrewLargeNegativeDay);
TESTCASE_AUTO(Test22730JapaneseOverflow);
TESTCASE_AUTO(Test22730CopticOverflow);
TESTCASE_AUTO(Test22962ComputeJulianDayOverflow);

TESTCASE_AUTO(TestAddOverflow);

Expand Down Expand Up @@ -5898,6 +5899,17 @@ void CalendarTest::Test22730CopticOverflow() {
assertEquals("status return without overflow", status, U_ILLEGAL_ARGUMENT_ERROR);
}

void CalendarTest::Test22962ComputeJulianDayOverflow() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<Calendar> calendar(
Calendar::createInstance(Locale("nds-NL-u-ca-islamic-umalqura"), status),
status);
calendar->clear();
calendar->set(UCAL_YEAR, -2147483648);
calendar->set(UCAL_WEEK_OF_YEAR, 33816240);
calendar->get(UCAL_ERA, status);
assertEquals("status return without overflow", status, U_ILLEGAL_ARGUMENT_ERROR);
}
void CalendarTest::TestAddOverflow() {
UErrorCode status = U_ZERO_ERROR;

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 @@ -348,6 +348,7 @@ class CalendarTest: public CalendarTimeZoneTest {
void Test22633RollTwiceGetTimeOverflow();
void Test22730JapaneseOverflow();
void Test22730CopticOverflow();
void Test22962ComputeJulianDayOverflow();

void Test22750Roll();

Expand Down

0 comments on commit 44ea927

Please sign in to comment.