ICU-22730 Fix Japanese extended year int32 overflow

This commit is contained in:
Frank Tang 2024-04-08 15:57:03 -07:00 committed by Frank Yung-Fong Tang
parent 943b0ca31b
commit 29b1141f79
3 changed files with 21 additions and 1 deletions

View file

@ -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);
}
/*

View file

@ -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);
@ -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;

View file

@ -346,6 +346,7 @@ public: // package
void Test22633SetRollGetTimeOverflow();
void Test22633AddTwiceGetTimeOverflow();
void Test22633RollTwiceGetTimeOverflow();
void Test22730JapaneseOverflow();
void RunTestOnCalendars(void(TestFunc)(Calendar*, UCalendarDateFields));
void verifyFirstDayOfWeek(const char* locale, UCalendarDaysOfWeek expected);