Skip to content

Zend: fix memory leak in array_map - #24138

Merged
arnaud-lb merged 1 commit into
php:PHP-8.6from
adapik:fix-memory-leak-array-map
Oct 15, 2026
Merged

arnaud-lb merged 1 commit into
php:PHP-8.6from
adapik:fix-memory-leak-array-map

Conversation

@adapik

@adapik adapik commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

zend_compile_func_array_map() takes an extra reference on a constant array argument because it is used by both ZEND_TYPE_ASSERT and ZEND_FE_RESET_R. For a non-interned string literal the refcount becomes 2, so the later zval_make_interned_string() copies the string and leaks the original:

 array_map(strtoupper(...), "str"); // TypeError, then a leak report

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct to me.
/cc @arnaud-lb

@LamentXU123
LamentXU123 requested a review from arnaud-lb October 5, 2026 15:37

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct, but I don't like that we are managing an invalid case. An alternative is to skip the optimization when we know that the array argument is not an array (return FAILURE). We can re-use the body of the if (!call_args) { block by moving it at the end of the function with a goto label.

@adapik

adapik commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@arnaud-lb I thought about this solution as well. I decided to do it this way, because I wanted to keep the fix surgical, but I agree suggested approach is much cleaner. I can implement these changes.

@adapik
adapik force-pushed the fix-memory-leak-array-map branch from 9f728b9 to 4843455 Compare October 9, 2026 14:42
@adapik
adapik requested a review from arnaud-lb October 9, 2026 14:42
@arnaud-lb
arnaud-lb merged commit e3ff9f1 into php:PHP-8.6 Oct 15, 2026
18 checks passed
arnaud-lb added a commit that referenced this pull request Oct 15, 2026
* PHP-8.6:
  Fix memory leak in array_map optimization (#24138)
@arnaud-lb

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants