diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index 3c6e5b06690..4b7d21ee13d 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -1571,6 +1571,7 @@ AliasReplacer::replaceTransformedExtensions( const char* tvalue = uprv_strchr(tkey, '-'); if (tvalue == nullptr) { status = U_ILLEGAL_ARGUMENT_ERROR; + return false; } const char* nextTKey = ultag_getTKeyStart(tvalue); if (nextTKey != nullptr) { @@ -1591,8 +1592,11 @@ AliasReplacer::replaceTransformedExtensions( } const char* tfield = (const char*) tfields.elementAt(i); const char* tvalue = uprv_strchr(tfield, '-'); + if (tvalue == nullptr) { + status = U_ILLEGAL_ARGUMENT_ERROR; + return false; + } // Split the "tkey-tvalue" pair string so that we can canonicalize the tvalue. - U_ASSERT(tvalue != nullptr); *((char*)tvalue++) = '\0'; // NULL terminate tkey output.append(tfield, status).append('-', status); const char* bcpTValue = ulocimp_toBcpType(tfield, tvalue, nullptr, nullptr); diff --git a/icu4c/source/test/intltest/loctest.cpp b/icu4c/source/test/intltest/loctest.cpp index 5503b008b0c..fc156f3ca3a 100644 --- a/icu4c/source/test/intltest/loctest.cpp +++ b/icu4c/source/test/intltest/loctest.cpp @@ -284,6 +284,7 @@ void LocaleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, c TESTCASE_AUTO(TestSetUnicodeKeywordValueNullInLongLocale); TESTCASE_AUTO(TestCanonicalize); TESTCASE_AUTO(TestLeak21419); + TESTCASE_AUTO(TestNullDereferenceWrite21597); TESTCASE_AUTO(TestLongLocaleSetKeywordAssign); TESTCASE_AUTO(TestLongLocaleSetKeywordMoveAssign); TESTCASE_AUTO_END; @@ -5030,7 +5031,6 @@ void LocaleTest::TestCanonicalize(void) // alias of tvalue should be replaced { "en-t-m0-NaMeS", "en-t-m0-prprname" }, { "en-t-s0-ascii-d0-NaMe", "en-t-d0-charname-s0-ascii" }, - }; int32_t i; for (i=0; i < UPRV_LENGTHOF(testCases); i++) { @@ -6586,3 +6586,10 @@ void LocaleTest::TestLeak21419() { l.canonicalize(status); status.expectErrorAndReset(U_ILLEGAL_ARGUMENT_ERROR); } + +void LocaleTest::TestNullDereferenceWrite21597() { + IcuTestErrorCode status(*this, "TestNullDereferenceWrite21597"); + Locale l = Locale("zu-t-q5-X1-vKf-KK-Ks-cO--Kc"); + l.canonicalize(status); + status.expectErrorAndReset(U_ILLEGAL_ARGUMENT_ERROR); +} diff --git a/icu4c/source/test/intltest/loctest.h b/icu4c/source/test/intltest/loctest.h index 12a93bde53d..4a856c62239 100644 --- a/icu4c/source/test/intltest/loctest.h +++ b/icu4c/source/test/intltest/loctest.h @@ -156,6 +156,7 @@ public: void TestSetUnicodeKeywordValueInLongLocale(); void TestSetUnicodeKeywordValueNullInLongLocale(); void TestLeak21419(); + void TestNullDereferenceWrite21597(); void TestLongLocaleSetKeywordAssign(); void TestLongLocaleSetKeywordMoveAssign();