Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22962 fix int32_t overflow inside handleComputeJulianDay #3265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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