ICU-21419 Fix memory leak in AliasReplacer

This commit is contained in:
Frank Tang 2020-11-30 18:00:14 -08:00 committed by Frank Yung-Fong Tang
parent d0096a84e7
commit 63cd3a7d40
3 changed files with 17 additions and 7 deletions

View file

@ -1410,7 +1410,7 @@ AliasReplacer::replaceTerritory(UVector& toBeFreed, UErrorCode& status)
.build(status);
l.addLikelySubtags(status);
const char* likelyRegion = l.getCountry();
CharString* item = nullptr;
LocalPointer<CharString> item;
if (likelyRegion != nullptr && uprv_strlen(likelyRegion) > 0) {
size_t len = uprv_strlen(likelyRegion);
const char* foundInReplacement = uprv_strstr(replacement,
@ -1422,20 +1422,22 @@ AliasReplacer::replaceTerritory(UVector& toBeFreed, UErrorCode& status)
*(foundInReplacement-1) == ' ');
U_ASSERT(foundInReplacement[len] == ' ' ||
foundInReplacement[len] == '\0');
item = new CharString(foundInReplacement, (int32_t)len, status);
item.adoptInsteadAndCheckErrorCode(
new CharString(foundInReplacement, (int32_t)len, status), status);
}
}
if (item == nullptr) {
item = new CharString(replacement,
(int32_t)(firstSpace - replacement), status);
if (item.isNull() && U_SUCCESS(status)) {
item.adoptInsteadAndCheckErrorCode(
new CharString(replacement,
(int32_t)(firstSpace - replacement), status), status);
}
if (U_FAILURE(status)) { return false; }
if (item == nullptr) {
if (item.isNull()) {
status = U_MEMORY_ALLOCATION_ERROR;
return false;
}
replacedRegion = item->data();
toBeFreed.addElement(item, status);
toBeFreed.addElement(item.orphan(), status);
}
U_ASSERT(!same(region, replacedRegion));
region = replacedRegion;

View file

@ -281,6 +281,7 @@ void LocaleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, c
TESTCASE_AUTO(TestSetUnicodeKeywordValueInLongLocale);
TESTCASE_AUTO(TestSetUnicodeKeywordValueNullInLongLocale);
TESTCASE_AUTO(TestCanonicalize);
TESTCASE_AUTO(TestLeak21419);
TESTCASE_AUTO_END;
}
@ -6406,3 +6407,9 @@ void LocaleTest::TestSetUnicodeKeywordValueNullInLongLocale() {
}
}
}
void LocaleTest::TestLeak21419() {
IcuTestErrorCode status(*this, "TestLeak21419");
Locale l = Locale("s-yU");
l.canonicalize(status);
}

View file

@ -153,6 +153,7 @@ public:
void TestCapturingTagConvertingIterator();
void TestSetUnicodeKeywordValueInLongLocale();
void TestSetUnicodeKeywordValueNullInLongLocale();
void TestLeak21419();
private:
void _checklocs(const char* label,