Skip to content

Commit 44ea927

Browse files
committed
ICU-22962 fix int32_t overflow inside handleComputeJulianDay
test
1 parent fbfbe6c commit 44ea927

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

icu4c/source/i18n/calendar.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3609,7 +3609,12 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField, UErrorCo
36093609
#endif
36103610
if(julianDay+testDate > nextJulianDay) { // is it past Dec 31? (nextJulianDay is day BEFORE year+1's Jan 1)
36113611
// Fire up the calculating engines.. retry YWOY = (year-1)
3612-
julianDay = handleComputeMonthStart(year-1, 0, false, status); // jd before Jan 1 of previous year
3612+
int32_t prevYear;
3613+
if (uprv_add32_overflow(year, -1, &prevYear)) {
3614+
status = U_ILLEGAL_ARGUMENT_ERROR;
3615+
return 0;
3616+
}
3617+
julianDay = handleComputeMonthStart(prevYear, 0, false, status); // jd before Jan 1 of previous year
36133618
if (U_FAILURE(status)) {
36143619
return 0;
36153620
}

icu4c/source/test/intltest/caltest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
207207
TESTCASE_AUTO(Test22633HebrewLargeNegativeDay);
208208
TESTCASE_AUTO(Test22730JapaneseOverflow);
209209
TESTCASE_AUTO(Test22730CopticOverflow);
210+
TESTCASE_AUTO(Test22962ComputeJulianDayOverflow);
210211

211212
TESTCASE_AUTO(TestAddOverflow);
212213

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

5902+
void CalendarTest::Test22962ComputeJulianDayOverflow() {
5903+
UErrorCode status = U_ZERO_ERROR;
5904+
LocalPointer<Calendar> calendar(
5905+
Calendar::createInstance(Locale("nds-NL-u-ca-islamic-umalqura"), status),
5906+
status);
5907+
calendar->clear();
5908+
calendar->set(UCAL_YEAR, -2147483648);
5909+
calendar->set(UCAL_WEEK_OF_YEAR, 33816240);
5910+
calendar->get(UCAL_ERA, status);
5911+
assertEquals("status return without overflow", status, U_ILLEGAL_ARGUMENT_ERROR);
5912+
}
59015913
void CalendarTest::TestAddOverflow() {
59025914
UErrorCode status = U_ZERO_ERROR;
59035915

icu4c/source/test/intltest/caltest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ class CalendarTest: public CalendarTimeZoneTest {
348348
void Test22633RollTwiceGetTimeOverflow();
349349
void Test22730JapaneseOverflow();
350350
void Test22730CopticOverflow();
351+
void Test22962ComputeJulianDayOverflow();
351352

352353
void Test22750Roll();
353354

0 commit comments

Comments
 (0)