Skip to content

ext/intl usmg_parse_helper possible mem leak fixes on failure path. #10830

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions ext/intl/intl_convertcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,41 @@ int intl_stringFromChar(UnicodeString &ret, char *str, size_t str_len, UErrorCod

zend_string* intl_charFromString(const UnicodeString &from, UErrorCode *status);

/**
* Utility class to act as a cleaner when an instance
* uses the expression when it goes out of its scope
* avoiding to needs handle all errors in all code paths
*
* note that template auto deduction is supported
* only from C++17, still need to be explicit
* at instantiation's time.
*/
template<typename T>
class intl_cleaner {
T expr;
bool useexpr{true};
public:
intl_cleaner(T& expr_) :
expr(std::move(expr_)) {}
intl_cleaner(const intl_cleaner& o) = delete;
intl_cleaner& operator=(const intl_cleaner& o) = delete;
// for the assignment constructors with std::move semantics
// we invalidate the original.
intl_cleaner(intl_cleaner&& o) :
expr(std::move(o.expr)),
useexpr(o.useexpr) {
o.useexpr = false;
}
intl_cleaner& operator=(const intl_cleaner&& o) {
if (&o == this)
return *this;
this->~intl_cleaner();
new (this)intl_cleaner(std::move(o));
return *this;
}
~intl_cleaner() {
if (useexpr)
expr();
}
};
#endif /* INTL_CONVERTCPP_H */
11 changes: 6 additions & 5 deletions ext/intl/msgformat/msgformat_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,13 @@ U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval **args, UCh
UnicodeString srcString(source, source_len);
Formattable *fargs = ((const MessageFormat*)fmt)->parse(srcString, *count, *status);

if(U_FAILURE(*status)) {
return;
}
if(U_FAILURE(*status)) {
return;
}

*args = (zval *)safe_emalloc(*count, sizeof(zval), 0);
*args = (zval *)safe_emalloc(*count, sizeof(zval), 0);
auto fargs_expr = [&]() { delete []fargs; };
auto fargs_cleaner = intl_cleaner<decltype(fargs_expr)>(fargs_expr);

// assign formattables to varargs
for(int32_t i = 0; i < *count; i++) {
Expand Down Expand Up @@ -676,5 +678,4 @@ U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval **args, UCh
break;
}
}
delete[] fargs;
}