]> granicus.if.org Git - icu/commitdiff
ICU-13127 Fix crashing bug in UnicodeString::toTitle()
authorAndy Heninger <andy.heninger@gmail.com>
Wed, 12 Apr 2017 01:19:22 +0000 (01:19 +0000)
committerAndy Heninger <andy.heninger@gmail.com>
Wed, 12 Apr 2017 01:19:22 +0000 (01:19 +0000)
X-SVN-Rev: 40036

icu4c/source/common/unistr_titlecase_brkiter.cpp
icu4c/source/test/intltest/strcase.cpp

index 6819049ddfefec76158402fa4a56350e183c0305..3156fdfc5754af9abc25477e6b19d622297ea373 100644 (file)
@@ -50,7 +50,11 @@ UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t
       return *this;
     }
   }
-  bi->setText(*this);
+  // Because the "this" string is both the source and the destination,
+  // make a copy of the original source for use by the break iterator.
+  // See tickets #13127 and #13128
+  UnicodeString copyOfInput(*this);
+  bi->setText(copyOfInput);
   caseMap(ustrcase_getCaseLocale(locale.getBaseName()), options, bi, ustrcase_internalToTitle);
   if(titleIter==NULL) {
     delete bi;
index a7c07c928fa15f53f19e5921c1d7e65da72ca3aa..5c291981d204dd95fdc917c664119d6d934f1958 100644 (file)
@@ -61,6 +61,7 @@ public:
     void TestCaseMapWithEdits();
     void TestCaseMapUTF8WithEdits();
     void TestLongUnicodeString();
+    void TestBug13127();
 
 private:
     void assertGreekUpper(const char16_t *s, const char16_t *expected);
@@ -89,6 +90,7 @@ StringCaseTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
     TESTCASE_AUTO(TestCaseConversion);
 #if !UCONFIG_NO_BREAK_ITERATION && !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
     TESTCASE_AUTO(TestCasing);
+    TESTCASE_AUTO(TestBug13127);
 #endif
     TESTCASE_AUTO(TestFullCaseFoldingIterator);
     TESTCASE_AUTO(TestGreekUpper);
@@ -1138,3 +1140,10 @@ void StringCaseTest::TestLongUnicodeString() {
     s.toUpper(Locale::getRoot());
     assertEquals("string length 306", expected, s);
 }
+
+void StringCaseTest::TestBug13127() {
+    // Test case crashed when the bug was present.
+    const char16_t *s16 = u"日本語";
+    UnicodeString s(TRUE, s16, -1);
+    s.toTitle(0, Locale::getEnglish());
+}