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

ReflectionFunction::getClosureUsedVariables() returns empty array in presence of variadic arguments #10623

Closed
fabio-ivona opened this issue Feb 19, 2023 · 3 comments

Comments

@fabio-ivona
Copy link
Contributor

fabio-ivona commented Feb 19, 2023

Description

It seems that when using a ReflectionFunction method getClosureUsedVariables() to retrieve its use variables, when the closure has a variadic argument, getClosureUsedVariables() returns an empty array

it can be reproduced (both in PHP8.1 and PHP8.2) by running this simple snipped

$data = 1;
$closure = function($var) use($data){};
var_dump((new ReflectionFunction($closure))->getClosureUsedVariables());

$closure = function($var, ...$variadic) use($data){};
var_dump((new ReflectionFunction($closure))->getClosureUsedVariables());

Resulted in this output:

array(1) {
  ["data"] => int(1)
}
array(0) {
}

But I expected this output instead:

array(1) {
  ["data"] => int(1)
}
array(0) {
  ["data"] => int(1)
}

PHP Version

PHP 8.2.2 (also in PHP 8.1.15 as well)

Operating System

tested in Ubuntu 20.04, 21.04 and alpine

Example snippet

https://onlinephp.io/c/1fd54

@fabio-ivona fabio-ivona changed the title ReflectionFunction::getClosureUsedVariables() empty with variadic arguments ReflectionFunction::getClosureUsedVariables() returns empty array in presence of variadic arguments Feb 19, 2023
@nielsdos
Copy link
Member

nielsdos commented Feb 19, 2023

Looks like it's implemented by reading the oplines, and I see that it explicitly uses num_args and BIND_STATIC (but no RECV_VARIADIC?), so I guess one of those two is where the problem lies. I'll try to look at this today.

@nielsdos
Copy link
Member

I think I fixed it locally by checking the ZEND_ACC_VARIADIC flag, and moving one opline forward to skip the RECV_VARIADIC. I'll check it to be sure, write a test and PR this up.

nielsdos added a commit to nielsdos/php-src that referenced this issue Feb 19, 2023
…s empty array in presence of variadic arguments

Fixes phpGH-10623
The code was missing the handling for the RECV_VARIADIC instruction.
nielsdos added a commit to nielsdos/php-src that referenced this issue Feb 19, 2023
Co-authored-by: Fabio Ivona <fabio.ivona@defstudio.it>
@fabio-ivona
Copy link
Contributor Author

Great! Thanks @nielsdos !

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

No branches or pull requests

3 participants
@nielsdos @fabio-ivona and others