From: Peter Edberg Date: Sun, 28 Feb 2016 23:48:41 +0000 (+0000) Subject: ICU-12154 C, LocaleDisplayNames should handle bad locale IDs without crashing X-Git-Tag: milestone-59-0-1~611 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d8540d0a324d76ba028019a99f1b707f6d2bf26;p=icu ICU-12154 C, LocaleDisplayNames should handle bad locale IDs without crashing X-SVN-Rev: 38427 --- diff --git a/icu4c/source/common/locdspnm.cpp b/icu4c/source/common/locdspnm.cpp index 952e66773ac..7bd4ecead48 100644 --- a/icu4c/source/common/locdspnm.cpp +++ b/icu4c/source/common/locdspnm.cpp @@ -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); } diff --git a/icu4c/source/test/intltest/locnmtst.cpp b/icu4c/source/test/intltest/locnmtst.cpp index ec583e23bd6..c3db5c7e26b 100644 --- a/icu4c/source/test/intltest/locnmtst.cpp +++ b/icu4c/source/test/intltest/locnmtst.cpp @@ -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 diff --git a/icu4c/source/test/intltest/locnmtst.h b/icu4c/source/test/intltest/locnmtst.h index 183de3bdd80..d4daf0cc734 100644 --- a/icu4c/source/test/intltest/locnmtst.h +++ b/icu4c/source/test/intltest/locnmtst.h @@ -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 };