From d7a6b3c01ed9242c21ed23436be3a6c45d54fd3f Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 6 Nov 2023 12:43:03 -0800 Subject: [PATCH] ICU-22568 return TimeZomeFormat::createInstance for bogus locale --- icu4c/source/i18n/tzfmt.cpp | 9 +++++---- icu4c/source/test/intltest/tzfmttst.cpp | 11 +++++++++++ icu4c/source/test/intltest/tzfmttst.h | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/icu4c/source/i18n/tzfmt.cpp b/icu4c/source/i18n/tzfmt.cpp index ed53438c4144..6891d3515f19 100644 --- a/icu4c/source/i18n/tzfmt.cpp +++ b/icu4c/source/i18n/tzfmt.cpp @@ -327,17 +327,18 @@ TimeZoneFormat::TimeZoneFormat(const Locale& locale, UErrorCode& status) const char* region = fLocale.getCountry(); int32_t regionLen = static_cast(uprv_strlen(region)); if (regionLen == 0) { + UErrorCode tempStatus = U_ZERO_ERROR; CharString loc; { CharStringByteSink sink(&loc); - ulocimp_addLikelySubtags(fLocale.getName(), sink, &status); + ulocimp_addLikelySubtags(fLocale.getName(), sink, &tempStatus); } - regionLen = uloc_getCountry(loc.data(), fTargetRegion, sizeof(fTargetRegion), &status); - if (U_SUCCESS(status)) { + regionLen = uloc_getCountry(loc.data(), fTargetRegion, sizeof(fTargetRegion), &tempStatus); + if (U_SUCCESS(tempStatus)) { fTargetRegion[regionLen] = 0; } else { - return; + fTargetRegion[0] = 0; } } else if (regionLen < (int32_t)sizeof(fTargetRegion)) { uprv_strcpy(fTargetRegion, region); diff --git a/icu4c/source/test/intltest/tzfmttst.cpp b/icu4c/source/test/intltest/tzfmttst.cpp index db79fc8a3d88..40a9b52c18a8 100644 --- a/icu4c/source/test/intltest/tzfmttst.cpp +++ b/icu4c/source/test/intltest/tzfmttst.cpp @@ -87,6 +87,7 @@ TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name TESTCASE(7, TestFormatTZDBNamesAllZoneCoverage); TESTCASE(8, TestAdoptDefaultThreadSafe); TESTCASE(9, TestCentralTime); + TESTCASE(10, TestBogusLocale); default: name = ""; break; } } @@ -1402,4 +1403,14 @@ TimeZoneFormatTest::TestCentralTime() { } } } +void +TimeZoneFormatTest::TestBogusLocale() { + Locale bogus("not a lang"); + UErrorCode status = U_ZERO_ERROR; + std::unique_ptr tzfmt( + icu::TimeZoneFormat::createInstance(bogus, status)); + if (U_FAILURE(status)) { + errln(u"Failed to createInstance with bogus locale"); + } +} #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/tzfmttst.h b/icu4c/source/test/intltest/tzfmttst.h index 8bbf5bc2b561..272b2b7832b0 100644 --- a/icu4c/source/test/intltest/tzfmttst.h +++ b/icu4c/source/test/intltest/tzfmttst.h @@ -31,6 +31,7 @@ class TimeZoneFormatTest : public IntlTest { void TestFormatTZDBNamesAllZoneCoverage(); void TestAdoptDefaultThreadSafe(); void TestCentralTime(); + void TestBogusLocale(); void RunTimeRoundTripTests(int32_t threadNumber); void RunAdoptDefaultThreadSafeTests(int32_t threadNumber);