From 47b321f1fa6a5bafcc69a8bb01215773a577fedc Mon Sep 17 00:00:00 2001 From: Frank Yung-Fong Tang Date: Mon, 13 Feb 2023 20:38:49 +0000 Subject: [PATCH] ICU-22277 correct collation error return of uninitialized length value while previous code return error See #2320 --- icu4c/source/i18n/ucol_res.cpp | 2 +- icu4c/source/test/intltest/regcoll.cpp | 21 +++++++++++++++++++++ icu4c/source/test/intltest/regcoll.h | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/icu4c/source/i18n/ucol_res.cpp b/icu4c/source/i18n/ucol_res.cpp index a955cef0e61..9bf28db264f 100644 --- a/icu4c/source/i18n/ucol_res.cpp +++ b/icu4c/source/i18n/ucol_res.cpp @@ -390,7 +390,7 @@ CollationLoader::loadFromData(UErrorCode &errorCode) { LocalUResourceBundlePointer binary(ures_getByKey(data, "%%CollationBin", nullptr, &errorCode)); // Note: U_MISSING_RESOURCE_ERROR --> The old code built from rules if available // but that created undesirable dependencies. - int32_t length; + int32_t length = 0; const uint8_t *inBytes = ures_getBinary(binary.getAlias(), &length, &errorCode); CollationDataReader::read(rootEntry->tailoring, inBytes, length, *t, errorCode); // Note: U_COLLATOR_VERSION_MISMATCH --> The old code built from rules if available diff --git a/icu4c/source/test/intltest/regcoll.cpp b/icu4c/source/test/intltest/regcoll.cpp index 5717f5bcf39..6a244ac5052 100644 --- a/icu4c/source/test/intltest/regcoll.cpp +++ b/icu4c/source/test/intltest/regcoll.cpp @@ -1249,6 +1249,26 @@ void CollationRegressionTest::TestBeforeWithTooStrongAfter() { } } +void CollationRegressionTest::TestICU22277() { + IcuTestErrorCode errorCode(*this, "TestICU22277"); + UErrorCode status = U_ZERO_ERROR; + + Collator* c = Collator::createInstance("JA-u-Co-priVatE-KANa", status); + if(c != nullptr || U_SUCCESS(status)) { + errcheckln(status, "Collator should have failed with MemorySanitizer: use-of-uninitialized-value error - %s", + u_errorName(status)); + delete c; + return; + } + c = Collator::createInstance("hE-U-cO-pRIVate-UNihan", status); + if(c != nullptr || U_SUCCESS(status)) { + errcheckln(status, "Collator should have failed with MemorySanitizer: use-of-uninitialized-value error - %s", + u_errorName(status)); + delete c; + return; + } +} + void CollationRegressionTest::compareArray(Collator &c, const char16_t tests[][CollationRegressionTest::MAX_TOKEN_LEN], int32_t testCount) @@ -1387,6 +1407,7 @@ void CollationRegressionTest::runIndexedTest(int32_t index, UBool exec, const ch TESTCASE_AUTO(TestCaseFirstCompression); TESTCASE_AUTO(TestTrailingComment); TESTCASE_AUTO(TestBeforeWithTooStrongAfter); + TESTCASE_AUTO(TestICU22277); TESTCASE_AUTO_END; } diff --git a/icu4c/source/test/intltest/regcoll.h b/icu4c/source/test/intltest/regcoll.h index 6a6de48a852..95735a71aa3 100644 --- a/icu4c/source/test/intltest/regcoll.h +++ b/icu4c/source/test/intltest/regcoll.h @@ -237,6 +237,9 @@ public: void TestTrailingComment(); void TestBeforeWithTooStrongAfter(); + // Test use-of-uninitialized-value + void TestICU22277(); + private: //------------------------------------------------------------------------ // Internal utilities