Skip to content

Fix array_map optimization with non-literal function or non-literal args - #23254

Draft
arnaud-lb wants to merge 1 commit into
php:masterfrom
arnaud-lb:pfa-bug-3
Draft

arnaud-lb wants to merge 1 commit into
php:masterfrom
arnaud-lb:pfa-bug-3

Conversation

@arnaud-lb

Copy link
Copy Markdown
Member

Non-literal expressions must be evaluated once and memoized to maintain semantics:

$callback = function ($value) {
    global $callback;
    $callback = function () { return 'changed'; };
    return $value + 1;
};

array_map($callback, [1,2]);

// Expected result: [2,3]
// Actual result: [2,'changed']

Unfortunately, we can't memoize pre-bound PFA arguments without breaking pass-by-reference, so we disable the optimization if the callback is a PFA with non-literal arguments. We could improve this when the function is known and we can determine that the argument is not passed by ref.

Bug found by Ryan @ Calif.io.

Comment thread Zend/zend_compile.c Outdated
Non-literal expressions must be evaluated once and memoized to maintain
semantics.
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.

2 participants