Skip to content

Commit

Permalink
Fix leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
catamorphism committed Oct 1, 2024
1 parent 8cae40c commit 71209d9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
19 changes: 17 additions & 2 deletions icu4c/source/i18n/messageformat2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ static Formattable evalLiteral(const Literal& lit) {
return FormattedPlaceholder(evalLiteral(lit), lit.quoted(), errorCode);
}


InternalValue::~InternalValue() {}
InternalValue& InternalValue::operator=(InternalValue&& other) {
fallbackString = other.fallbackString;
val = std::move(other.val);
return *this;
}

InternalValue::InternalValue(InternalValue&& other) {
*this = std::move(other);
}

[[nodiscard]] InternalValue MessageFormatter::formatOperand(const Environment& env,
const Operand& rand,
MessageContext& context,
Expand Down Expand Up @@ -658,14 +670,17 @@ ResolvedSelector MessageFormatter::resolveVariables(const Environment& env,
// Already checked that rator is non-reserved
const FunctionName& selectorName = rator->getFunctionName();
if (isSelector(selectorName)) {
auto selector = getSelector(context, selectorName, status);
LocalPointer<Selector> selector(getSelector(context, selectorName, status));
if (U_SUCCESS(status)) {
FunctionOptions resolvedOptions = resolveOptions(env, rator->getOptionsInternal(), context, status);
InternalValue argument = formatOperand(env, expr.getOperand(), context, status);
if (argument.isFallback()) {
return ResolvedSelector(argument.asFallback());
} else {
return ResolvedSelector(selectorName, selector, std::move(resolvedOptions), argument.value());
return ResolvedSelector(selectorName,
selector.orphan(),
std::move(resolvedOptions),
argument.value());
}
}
} else if (isFormatter(selectorName)) {
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/i18n/messageformat2_evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ FunctionOptions::FunctionOptions(FunctionOptions&& other) {
FunctionOptions::~FunctionOptions() {
if (options != nullptr) {
delete[] options;
options = nullptr;
}
}
// ResolvedSelector
Expand Down
3 changes: 3 additions & 0 deletions icu4c/source/i18n/messageformat2_formattable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ namespace message2 {
if (type == kEvaluated) {
formatted = std::move(other.formatted);
}
if (previousOptions != nullptr) {
delete previousOptions;
}
if (other.previousOptions != nullptr) {
previousOptions = other.previousOptions;
other.previousOptions = nullptr;
Expand Down
3 changes: 3 additions & 0 deletions icu4c/source/i18n/unicode/messageformat2.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace message2 {
: fallbackString(""), val(std::move(f)) {}
FormattedPlaceholder value() { return std::move(val); }
UnicodeString asFallback() const { return fallbackString; }
virtual ~InternalValue();
InternalValue& operator=(InternalValue&&);
InternalValue(InternalValue&&);
private:
UnicodeString fallbackString; // Non-empty if fallback
// Otherwise, assumed to be a FormattedPlaceholder
Expand Down

0 comments on commit 71209d9

Please sign in to comment.