From 3fb3929f80370f974f26c40015fed91c626594d5 Mon Sep 17 00:00:00 2001 From: Elango Cheran Date: Fri, 10 Apr 2020 11:22:56 -0700 Subject: [PATCH] ICU-21040 Fix segfaults in no data tests --- icu4c/source/i18n/number_formatimpl.cpp | 18 ++++++++++++++++++ icu4c/source/test/intltest/dtifmtts.cpp | 3 +++ icu4c/source/test/intltest/numfmtst.cpp | 6 ++++++ icu4c/source/test/intltest/transtst.cpp | 3 +++ 4 files changed, 30 insertions(+) diff --git a/icu4c/source/i18n/number_formatimpl.cpp b/icu4c/source/i18n/number_formatimpl.cpp index 8042979bdc..5bba09cfb5 100644 --- a/icu4c/source/i18n/number_formatimpl.cpp +++ b/icu4c/source/i18n/number_formatimpl.cpp @@ -203,6 +203,9 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe, patternStyle = CLDR_PATTERN_STYLE_CURRENCY; } pattern = utils::getPatternForStyle(macros.locale, nsName, patternStyle, status); + if (U_FAILURE(status)) { + return nullptr; + } } auto patternInfo = new ParsedPatternInfo(); if (patternInfo == nullptr) { @@ -211,6 +214,9 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe, } fPatternInfo.adoptInstead(patternInfo); PatternParser::parseToPatternInfo(UnicodeString(pattern), *patternInfo, status); + if (U_FAILURE(status)) { + return nullptr; + } ///////////////////////////////////////////////////////////////////////////////////// /// START POPULATING THE DEFAULT MICROPROPS AND BUILDING THE MICROPROPS GENERATOR /// @@ -241,6 +247,9 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe, roundingMode = precision.fRoundingMode; } fMicros.rounder = {precision, roundingMode, currency, status}; + if (U_FAILURE(status)) { + return nullptr; + } // Grouping strategy if (!macros.grouper.isBogus()) { @@ -323,6 +332,9 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe, if (safe) { fImmutablePatternModifier.adoptInstead(patternModifier->createImmutable(status)); } + if (U_FAILURE(status)) { + return nullptr; + } // Outer modifier (CLDR units and currency long names) if (isCldrUnit) { @@ -349,6 +361,9 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe, // No outer modifier required fMicros.modOuter = &fMicros.helpers.emptyWeakModifier; } + if (U_FAILURE(status)) { + return nullptr; + } // Compact notation if (macros.notation.fType == Notation::NTN_COMPACT) { @@ -371,6 +386,9 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe, fCompactHandler.adoptInstead(newCompactHandler); chain = fCompactHandler.getAlias(); } + if (U_FAILURE(status)) { + return nullptr; + } // Always add the pattern modifier as the last element of the chain. if (safe) { diff --git a/icu4c/source/test/intltest/dtifmtts.cpp b/icu4c/source/test/intltest/dtifmtts.cpp index 2f59c1905c..9713dee899 100644 --- a/icu4c/source/test/intltest/dtifmtts.cpp +++ b/icu4c/source/test/intltest/dtifmtts.cpp @@ -1943,6 +1943,9 @@ void DateIntervalFormatTest::testTicket20707() { int32_t j = 0; for (const UnicodeString skeleton : {u"hh", u"HH", u"kk", u"KK", u"jj", u"JJs", u"CC"}) { LocalPointer dtifmt(DateIntervalFormat::createInstance(skeleton, locale, status)); + if (status.errDataIfFailureAndReset()) { + continue; + } FieldPosition fposition; UnicodeString result; LocalPointer calendar(Calendar::createInstance(TimeZone::createTimeZone(timeZone), status)); diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 62e161f570..5a26a7a560 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -9679,6 +9679,9 @@ void NumberFormatTest::Test20956_MonetarySymbolGetters() { IcuTestErrorCode status(*this, "Test20956_MonetarySymbolGetters"); LocalPointer decimalFormat(static_cast( NumberFormat::createCurrencyInstance("et", status))); + if (status.errDataIfFailureAndReset()) { + return; + } decimalFormat->setCurrency(u"EEK"); @@ -9823,6 +9826,9 @@ void NumberFormatTest::Test20961_CurrencyPluralPattern() { { LocalPointer decimalFormat(static_cast( NumberFormat::createInstance("en-US", UNUM_CURRENCY_PLURAL, status))); + if (status.errDataIfFailureAndReset()) { + return; + } UnicodeString result; decimalFormat->toPattern(result); assertEquals("Currency pattern", u"#,##0.00 ¤¤¤", result); diff --git a/icu4c/source/test/intltest/transtst.cpp b/icu4c/source/test/intltest/transtst.cpp index fd7f733a91..8e7bcb0917 100644 --- a/icu4c/source/test/intltest/transtst.cpp +++ b/icu4c/source/test/intltest/transtst.cpp @@ -1562,6 +1562,7 @@ void TransliteratorTest::TestBasicTransliteratorEvenWithoutData() { BASIC_TRANSLITERATOR_ID[i], UTRANS_FORWARD, parseError, status)); if (translit.get() == nullptr || !U_SUCCESS(status)) { dataerrln("FAIL: createInstance %s failed", BASIC_TRANSLITERATOR_ID[i]); + continue; } UnicodeString data(TEST_DATA); UnicodeString expected(EXPECTED_RESULTS[i]); @@ -1570,6 +1571,7 @@ void TransliteratorTest::TestBasicTransliteratorEvenWithoutData() { dataerrln(UnicodeString("FAIL: expected translit(") + BASIC_TRANSLITERATOR_ID[i] + ") = '" + EXPECTED_RESULTS[i] + "' but got '" + data); + continue; } } for (int32_t i=0; BASIC_TRANSLITERATOR_RULES[i]; i++) { @@ -1580,6 +1582,7 @@ void TransliteratorTest::TestBasicTransliteratorEvenWithoutData() { BASIC_TRANSLITERATOR_RULES[i], UTRANS_FORWARD, parseError, status)); if (translit.get() == nullptr || !U_SUCCESS(status)) { dataerrln("FAIL: createFromRules %s failed", BASIC_TRANSLITERATOR_RULES[i]); + continue; } } }