ICU-12154 C, LocaleDisplayNames should handle bad locale IDs without crashing

X-SVN-Rev: 38427
This commit is contained in:
Peter Edberg 2016-02-28 23:48:41 +00:00
parent f01caf199b
commit 2d8540d0a3
3 changed files with 23 additions and 3 deletions

View file

@ -540,6 +540,10 @@ LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage,
UnicodeString&
LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale,
UnicodeString& result) const {
if (locale.isBogus()) {
result.setToBogus();
return result;
}
UnicodeString resultName;
const char* lang = locale.getLanguage();
@ -902,6 +906,10 @@ uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
}
UnicodeString temp(result, 0, maxResultSize);
((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
if (temp.isBogus()) {
*pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return temp.extract(result, maxResultSize, *pErrorCode);
}

View file

@ -78,6 +78,7 @@ void LocaleDisplayNamesTest::runIndexedTest(int32_t index, UBool exec, const cha
TESTCASE(10, TestUntranslatedKeywords);
TESTCASE(11, TestPrivateUse);
TESTCASE(12, TestUldnDisplayContext);
TESTCASE(13, TestUldnWithGarbage);
#endif
default:
name = "";
@ -188,6 +189,16 @@ void LocaleDisplayNamesTest::TestUldnOpenDialect() {
test_assert_equal("British English", str);
}
void LocaleDisplayNamesTest::TestUldnWithGarbage() {
UErrorCode status = U_ZERO_ERROR;
const int32_t kMaxResultSize = 150; // long enough
UChar result[150];
ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
int32_t len = uldn_localeDisplayName(ldn, "english (United States) [w", result, kMaxResultSize, &status);
uldn_close(ldn);
test_assert(U_FAILURE(status));
}
void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
UErrorCode status = U_ZERO_ERROR;
const int32_t kMaxResultSize = 150; // long enough

View file

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2010-2014, International Business Machines Corporation
* COPYRIGHT:
* Copyright (c) 2010-2016, International Business Machines Corporation
* and others. All Rights Reserved.
********************************************************************/
@ -14,7 +14,7 @@ class LocaleDisplayNamesTest: public IntlTest {
public:
LocaleDisplayNamesTest();
virtual ~LocaleDisplayNamesTest();
void runIndexedTest(int32_t index, UBool exec, const char* &name, char* par = NULL);
#if !UCONFIG_NO_FORMATTING
@ -34,5 +34,6 @@ public:
void TestUntranslatedKeywords(void);
void TestPrivateUse(void);
void TestUldnDisplayContext(void);
void TestUldnWithGarbage(void);
#endif
};