Skip to content

Commit

Permalink
ICU-22730 Fix Japanese extended year int32 overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Apr 11, 2024
1 parent 943b0ca commit 29b1141
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion icu4c/source/i18n/japancal.cpp
Expand Up @@ -227,8 +227,16 @@ void JapaneseCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status
int32_t year = internalGet(UCAL_EXTENDED_YEAR); // Gregorian year
int32_t eraIdx = gJapaneseEraRules->getEraIndex(year, internalGetMonth(status) + 1, internalGet(UCAL_DAY_OF_MONTH), status);

int32_t startYear = gJapaneseEraRules->getStartYear(eraIdx, status) - 1;
if (U_FAILURE(status)) {
return;
}
if (uprv_add32_overflow(year, -startYear, &year)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
internalSet(UCAL_ERA, eraIdx);
internalSet(UCAL_YEAR, year - gJapaneseEraRules->getStartYear(eraIdx, status) + 1);
internalSet(UCAL_YEAR, year);
}

/*
Expand Down
11 changes: 11 additions & 0 deletions icu4c/source/test/intltest/caltest.cpp
Expand Up @@ -205,6 +205,7 @@ void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
TESTCASE_AUTO(Test22633RollTwiceGetTimeOverflow);

TESTCASE_AUTO(Test22633HebrewLargeNegativeDay);
TESTCASE_AUTO(Test22730JapaneseOverflow);

TESTCASE_AUTO(TestAddOverflow);

Expand Down Expand Up @@ -5876,6 +5877,16 @@ void CalendarTest::Test22633HebrewLargeNegativeDay() {
assertEquals("status return without hang", status, U_ILLEGAL_ARGUMENT_ERROR);
}

void CalendarTest::Test22730JapaneseOverflow() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<Calendar> calendar(
Calendar::createInstance(Locale("en-u-ca-japanese"), status),
status);
calendar->clear();
calendar->roll(UCAL_EXTENDED_YEAR, -1946156856, 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
Expand Up @@ -346,6 +346,7 @@ class CalendarTest: public CalendarTimeZoneTest {
void Test22633SetRollGetTimeOverflow();
void Test22633AddTwiceGetTimeOverflow();
void Test22633RollTwiceGetTimeOverflow();
void Test22730JapaneseOverflow();
void RunTestOnCalendars(void(TestFunc)(Calendar*, UCalendarDateFields));

void verifyFirstDayOfWeek(const char* locale, UCalendarDaysOfWeek expected);
Expand Down

0 comments on commit 29b1141

Please sign in to comment.