diff --git a/icu4c/source/common/uniset.cpp b/icu4c/source/common/uniset.cpp index 2d6610cb355..0a63dad9dd0 100644 --- a/icu4c/source/common/uniset.cpp +++ b/icu4c/source/common/uniset.cpp @@ -974,12 +974,12 @@ void UnicodeSet::_add(const UnicodeString& s) { setToBogus(); return; } - UnicodeString* t = new UnicodeString(s); - if (t == nullptr) { // Check for memory allocation error. + LocalPointer t(new UnicodeString(s)); + if (t.isNull()) { // Check for memory allocation error. setToBogus(); return; } - strings_->sortedInsert(t, compareUnicodeString, ec); + strings_->sortedInsert(t.orphan(), compareUnicodeString, ec); if (U_FAILURE(ec)) { setToBogus(); } diff --git a/icu4c/source/i18n/rbt_pars.cpp b/icu4c/source/i18n/rbt_pars.cpp index 9dfdbfe1e9d..e2dbb058d5c 100644 --- a/icu4c/source/i18n/rbt_pars.cpp +++ b/icu4c/source/i18n/rbt_pars.cpp @@ -535,15 +535,14 @@ int32_t RuleHalf::parseSection(const UnicodeString& rule, int32_t pos, int32_t l // in buf, starting at offset bufSegStart. Extract them // into a string matcher, and replace them with a // standin for that matcher. - StringMatcher* m = - new StringMatcher(buf, bufSegStart, buf.length(), - segmentNumber, *parser.curData); - if (m == nullptr) { + LocalPointer m(new StringMatcher(buf, bufSegStart, buf.length(), + segmentNumber, *parser.curData), status); + if (U_FAILURE(status)) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } // Record and associate object and segment number - parser.setSegmentObject(segmentNumber, m, status); + parser.setSegmentObject(segmentNumber, m.orphan(), status); buf.truncate(bufSegStart); buf.append(parser.getSegmentStandin(segmentNumber, status)); } @@ -577,15 +576,15 @@ int32_t RuleHalf::parseSection(const UnicodeString& rule, int32_t pos, int32_t l // in buf, starting at offset bufSegStart. UnicodeString output; buf.extractBetween(bufSegStart, buf.length(), output); - FunctionReplacer *r = - new FunctionReplacer(t, new StringReplacer(output, parser.curData)); - if (r == nullptr) { + LocalPointer r( + new FunctionReplacer(t, new StringReplacer(output, parser.curData)), status); + if (U_FAILURE(status)) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } // Replace the buffer contents with a stand-in buf.truncate(bufSegStart); - buf.append(parser.generateStandInFor(r, status)); + buf.append(parser.generateStandInFor(r.orphan(), status)); } break; case SymbolTable::SYMBOL_REF: @@ -671,9 +670,9 @@ int32_t RuleHalf::parseSection(const UnicodeString& rule, int32_t pos, int32_t l qlimit = qstart + 1; } - UnicodeFunctor *m = - new StringMatcher(buf, qstart, qlimit, 0, *parser.curData); - if (m == nullptr) { + LocalPointer m( + new StringMatcher(buf, qstart, qlimit, 0, *parser.curData), status); + if (U_FAILURE(status)) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } int32_t min = 0; @@ -689,12 +688,16 @@ int32_t RuleHalf::parseSection(const UnicodeString& rule, int32_t pos, int32_t l // case KLEENE_STAR: // do nothing -- min, max already set } - m = new Quantifier(m, min, max); - if (m == nullptr) { + LocalPointer m2(new Quantifier(m.getAlias(), min, max), status); + if (m2.isValid()) { + m.orphan(); + } + if (U_FAILURE(status)) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } + m = std::move(m2); buf.truncate(qstart); - buf.append(parser.generateStandInFor(m, status)); + buf.append(parser.generateStandInFor(m.orphan(), status)); } break; @@ -921,7 +924,7 @@ void TransliteratorParser::parseRules(const UnicodeString& rule, dotStandIn = static_cast(-1); - UnicodeString *tempstr = nullptr; // used for memory allocation error checking + LocalPointer tempstr; // used for memory allocation error checking UnicodeString str; // scratch UnicodeString idBlockResult; int32_t pos = 0; @@ -1029,17 +1032,16 @@ void TransliteratorParser::parseRules(const UnicodeString& rule, pos = p; } else { if (parsingIDs) { - tempstr = new UnicodeString(idBlockResult); + tempstr.adoptInsteadAndCheckErrorCode(new UnicodeString(idBlockResult), status); // nullptr pointer check - if (tempstr == nullptr) { - status = U_MEMORY_ALLOCATION_ERROR; + if (U_FAILURE(status)) { return; } U_ASSERT(idBlockVector.hasDeleter()); if (direction == UTRANS_FORWARD) - idBlockVector.adoptElement(tempstr, status); + idBlockVector.adoptElement(tempstr.orphan(), status); else - idBlockVector.insertElementAt(tempstr, 0, status); + idBlockVector.insertElementAt(tempstr.orphan(), 0, status); if (U_FAILURE(status)) { return; } @@ -1074,18 +1076,17 @@ void TransliteratorParser::parseRules(const UnicodeString& rule, } if (parsingIDs && idBlockResult.length() > 0) { - tempstr = new UnicodeString(idBlockResult); + tempstr.adoptInsteadAndCheckErrorCode(new UnicodeString(idBlockResult), status); // nullptr pointer check - if (tempstr == nullptr) { + if (U_FAILURE(status)) { // TODO: Testing, forcing this path, shows many memory leaks. ICU-21701 // intltest translit/TransliteratorTest/TestInstantiation - status = U_MEMORY_ALLOCATION_ERROR; return; } if (direction == UTRANS_FORWARD) - idBlockVector.adoptElement(tempstr, status); + idBlockVector.adoptElement(tempstr.orphan(), status); else - idBlockVector.insertElementAt(tempstr, 0, status); + idBlockVector.insertElementAt(tempstr.orphan(), 0, status); if (U_FAILURE(status)) { return; } @@ -1365,12 +1366,12 @@ int32_t TransliteratorParser::parseRule(const UnicodeString& rule, int32_t pos, return syntaxError(U_MALFORMED_VARIABLE_DEFINITION, rule, start, status); } // We allow anything on the right, including an empty string. - UnicodeString* value = new UnicodeString(right->text); + LocalPointer value(new UnicodeString(right->text), status); // nullptr pointer check - if (value == nullptr) { + if (U_FAILURE(status)) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } - variableNames.put(undefinedVariableName, value, status); + variableNames.put(undefinedVariableName, value.orphan(), status); ++variableLimit; return pos; } @@ -1451,30 +1452,32 @@ int32_t TransliteratorParser::parseRule(const UnicodeString& rule, int32_t pos, } // Flatten segment objects vector to an array - UnicodeFunctor** segmentsArray = nullptr; + LocalMemory segmentsArray; if (segmentObjects.size() > 0) { - segmentsArray = static_cast(uprv_malloc(segmentObjects.size() * sizeof(UnicodeFunctor*))); + segmentsArray.adoptInstead(static_cast(uprv_malloc(segmentObjects.size() * sizeof(UnicodeFunctor*)))); // Null pointer check - if (segmentsArray == nullptr) { + if (segmentsArray.isNull()) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } - segmentObjects.toArray(reinterpret_cast(segmentsArray)); + segmentObjects.toArray(reinterpret_cast(segmentsArray.getAlias())); } - TransliterationRule* temptr = new TransliterationRule( + LocalPointer temptr(new TransliterationRule( left->text, left->ante, left->post, right->text, right->cursor, right->cursorOffset, - segmentsArray, + segmentsArray.getAlias(), segmentObjects.size(), left->anchorStart, left->anchorEnd, curData, - status); + status), status); //Null pointer check - if (temptr == nullptr) { - uprv_free(segmentsArray); + if (temptr.isValid()) { + segmentsArray.orphan(); + } + if (U_FAILURE(status)) { return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); } - curData->ruleSet.addRule(temptr, status); + curData->ruleSet.addRule(temptr.orphan(), status); return pos; } @@ -1620,13 +1623,12 @@ void TransliteratorParser::setSegmentObject(int32_t seg, StringMatcher* adopted, */ char16_t TransliteratorParser::getDotStandIn(UErrorCode& status) { if (dotStandIn == static_cast(-1)) { - UnicodeSet* tempus = new UnicodeSet(UnicodeString(true, DOT_SET, -1), status); + LocalPointer tempus(new UnicodeSet(UnicodeString(true, DOT_SET, -1), status), status); // Null pointer check. - if (tempus == nullptr) { - status = U_MEMORY_ALLOCATION_ERROR; + if (U_FAILURE(status)) { return static_cast(0x0000); } - dotStandIn = generateStandInFor(tempus, status); + dotStandIn = generateStandInFor(tempus.orphan(), status); } return dotStandIn; } diff --git a/icu4c/source/i18n/tznames_impl.cpp b/icu4c/source/i18n/tznames_impl.cpp index 769c9a6526c..c24561fa1ac 100644 --- a/icu4c/source/i18n/tznames_impl.cpp +++ b/icu4c/source/i18n/tznames_impl.cpp @@ -715,15 +715,15 @@ private: for (int32_t i = 0; i < UTZNM_INDEX_COUNT; i++) { const char16_t* name = fNames[i]; if (name != nullptr) { - ZNameInfo* nameinfo = static_cast(uprv_malloc(sizeof(ZNameInfo))); - if (nameinfo == nullptr) { + LocalMemory nameinfo(static_cast(uprv_malloc(sizeof(ZNameInfo)))); + if (nameinfo.isNull()) { status = U_MEMORY_ALLOCATION_ERROR; return; } nameinfo->mzID = mzID; nameinfo->tzID = tzID; nameinfo->type = getTZNameType(static_cast(i)); - trie.put(name, nameinfo, status); // trie.put() takes ownership of the key + trie.put(name, nameinfo.orphan(), status); // trie.put() takes ownership of the key if (U_FAILURE(status)) { return; }