Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22830 Fix memLeak in numrange_fluent.cpp #3085

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions icu4c/source/i18n/numrange_fluent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ FormattedNumberRange LocalizedNumberRangeFormatter::formatFormattableRange(
return FormattedNumberRange(U_ILLEGAL_ARGUMENT_ERROR);
}

auto* results = new UFormattedNumberRangeData();
if (results == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
LocalPointer<UFormattedNumberRangeData> results(new UFormattedNumberRangeData(), status);
if (U_FAILURE(status)) {
return FormattedNumberRange(status);
}

Expand All @@ -333,9 +332,8 @@ FormattedNumberRange LocalizedNumberRangeFormatter::formatFormattableRange(

// Do not save the results object if we encountered a failure.
if (U_SUCCESS(status)) {
return FormattedNumberRange(results);
return FormattedNumberRange(results.orphan());
} else {
delete results;
return FormattedNumberRange(status);
}
}
Expand Down Expand Up @@ -373,27 +371,21 @@ LocalizedNumberRangeFormatter::getFormatter(UErrorCode& status) const {
}

// Try computing the formatter on our own
auto* temp = new NumberRangeFormatterImpl(fMacros, status);
LocalPointer<NumberRangeFormatterImpl> temp(new NumberRangeFormatterImpl(fMacros, status), status);
if (U_FAILURE(status)) {
delete temp;
return nullptr;
}
if (temp == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}

// Note: ptr starts as nullptr; during compare_exchange,
// it is set to what is actually stored in the atomic
// if another thread beat us to computing the formatter object.
auto* nonConstThis = const_cast<LocalizedNumberRangeFormatter*>(this);
if (!nonConstThis->fAtomicFormatter.compare_exchange_strong(ptr, temp)) {
if (!nonConstThis->fAtomicFormatter.compare_exchange_strong(ptr, temp.getAlias())) {
// Another thread beat us to computing the formatter
delete temp;
return ptr;
} else {
// Our copy of the formatter got stored in the atomic
return temp;
return temp.orphan();
}

}
Expand Down