From f07320ca1b7473b36bdc2b6cbc0841d7f024ae4e Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Wed, 27 Nov 2024 13:40:54 -0800 Subject: [PATCH] ICU-22983 Fix DecimalQuantity::shiftLeft ubsan issue --- icu4c/source/i18n/number_decimalquantity.cpp | 2 +- icu4c/source/test/intltest/numfmtst.cpp | 5 +++++ icu4c/source/test/intltest/numfmtst.h | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/icu4c/source/i18n/number_decimalquantity.cpp b/icu4c/source/i18n/number_decimalquantity.cpp index f9350d5d5cc8..ca1dacd3579d 100644 --- a/icu4c/source/i18n/number_decimalquantity.cpp +++ b/icu4c/source/i18n/number_decimalquantity.cpp @@ -1133,7 +1133,7 @@ void DecimalQuantity::setDigitPos(int32_t position, int8_t value) { } void DecimalQuantity::shiftLeft(int32_t numDigits) { - if (!usingBytes && precision + numDigits > 16) { + if (!usingBytes && precision + numDigits >= 16) { switchStorage(); } if (usingBytes) { diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index a37111a8540d..ede924722952 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -253,6 +253,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n TESTCASE_AUTO(Test10997_FormatCurrency); TESTCASE_AUTO(Test21556_CurrencyAsDecimal); TESTCASE_AUTO(Test22088_Ethiopic); + TESTCASE_AUTO(Test22983_LongFraction); TESTCASE_AUTO_END; } @@ -10161,5 +10162,9 @@ void NumberFormatTest::Test22088_Ethiopic() { assertEquals("Wrong result with UNUM_NUMBERING_SYSTEM and English", u"123", nf3->format(123, result)); } } +void NumberFormatTest::Test22983_LongFraction() { + IcuTestErrorCode status(*this, "Test22983_LongFraction"); + DecimalFormat df(u"0.0000000000000001", status); +} #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index 5bdbc131cda3..10dd3386d4c9 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -309,6 +309,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void Test10997_FormatCurrency(); void Test21556_CurrencyAsDecimal(); void Test22088_Ethiopic(); + void Test22983_LongFraction(); private: UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f);