]> granicus.if.org Git - icu/commitdiff
ICU-12154 C, LocaleDisplayNames should handle bad locale IDs without crashing
authorPeter Edberg <pedberg@unicode.org>
Sun, 28 Feb 2016 23:48:41 +0000 (23:48 +0000)
committerPeter Edberg <pedberg@unicode.org>
Sun, 28 Feb 2016 23:48:41 +0000 (23:48 +0000)
X-SVN-Rev: 38427

icu4c/source/common/locdspnm.cpp
icu4c/source/test/intltest/locnmtst.cpp
icu4c/source/test/intltest/locnmtst.h

index 952e66773ac292119780d0ac87a7ffca1c761da8..7bd4ecead48d9b8288a28d24c9ee2769bb64a545 100644 (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);
 }
 
index ec583e23bd6a85fe1bf9be75cb5e7e207c3aafd0..c3db5c7e26b726c872f46e40f2229ae51c5362c7 100644 (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
index 183de3bdd80856fdccf0e7ba553f8f7f786bf1f6..d4daf0cc7347a44e6fd4c58c6efa6cd1754e099e 100644 (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
 };