Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22568 return TimeZomeFormat::createInstance for bogus locale #2696

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions icu4c/source/i18n/tzfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,18 @@ TimeZoneFormat::TimeZoneFormat(const Locale& locale, UErrorCode& status)
const char* region = fLocale.getCountry();
int32_t regionLen = static_cast<int32_t>(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);
Expand Down
11 changes: 11 additions & 0 deletions icu4c/source/test/intltest/tzfmttst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -1402,4 +1403,14 @@ TimeZoneFormatTest::TestCentralTime() {
}
}
}
void
TimeZoneFormatTest::TestBogusLocale() {
Locale bogus("not a lang");
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<icu::TimeZoneFormat> tzfmt(
icu::TimeZoneFormat::createInstance(bogus, status));
if (U_FAILURE(status)) {
errln(u"Failed to createInstance with bogus locale");
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/tzfmttst.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading