ICU-22849 Fix memLeak in RBBIRuleBuilder by using LocalMemory

This commit is contained in:
Frank Tang 2024-08-07 15:34:16 -07:00 committed by Frank Yung-Fong Tang
parent b5b3e16afa
commit 0bd2b4d10c

View file

@ -182,12 +182,12 @@ RBBIDataHeader *RBBIRuleBuilder::flattenData() {
}
#endif
RBBIDataHeader* data = static_cast<RBBIDataHeader*>(uprv_malloc(totalSize));
if (data == nullptr) {
LocalMemory<RBBIDataHeader> data(static_cast<RBBIDataHeader*>(uprv_malloc(totalSize)));
if (data.isNull()) {
*fStatus = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
uprv_memset(data, 0, totalSize);
uprv_memset(data.getAlias(), 0, totalSize);
data->fMagic = 0xb1a0;
@ -213,23 +213,23 @@ RBBIDataHeader *RBBIRuleBuilder::flattenData() {
uprv_memset(data->fReserved, 0, sizeof(data->fReserved));
fForwardTable->exportTable(reinterpret_cast<uint8_t*>(data) + data->fFTable);
fForwardTable->exportSafeTable(reinterpret_cast<uint8_t*>(data) + data->fRTable);
fSetBuilder->serializeTrie(reinterpret_cast<uint8_t*>(data) + data->fTrie);
fForwardTable->exportTable(reinterpret_cast<uint8_t*>(data.getAlias()) + data->fFTable);
fForwardTable->exportSafeTable(reinterpret_cast<uint8_t*>(data.getAlias()) + data->fRTable);
fSetBuilder->serializeTrie(reinterpret_cast<uint8_t*>(data.getAlias()) + data->fTrie);
int32_t* ruleStatusTable = reinterpret_cast<int32_t*>(reinterpret_cast<uint8_t*>(data) + data->fStatusTable);
int32_t* ruleStatusTable = reinterpret_cast<int32_t*>(reinterpret_cast<uint8_t*>(data.getAlias()) + data->fStatusTable);
for (i=0; i<fRuleStatusVals->size(); i++) {
ruleStatusTable[i] = fRuleStatusVals->elementAti(i);
}
u_strToUTF8WithSub(reinterpret_cast<char*>(data) + data->fRuleSource, rulesSize, &rulesLengthInUTF8,
u_strToUTF8WithSub(reinterpret_cast<char*>(data.getAlias()) + data->fRuleSource, rulesSize, &rulesLengthInUTF8,
fStrippedRules.getBuffer(), fStrippedRules.length(),
0xfffd, nullptr, fStatus);
if (U_FAILURE(*fStatus)) {
return nullptr;
}
return data;
return data.orphan();
}