[php-src] Issue #9965: Non-pure default arguments are mistakenly cached
| From: | iluuu1994 | Date: | Thu, 17 Nov 2022 10:53:47 +0000 |
| Subject: | [php-src] Issue #9965: Non-pure default arguments are mistakenly cached | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242835@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9965
Author: iluuu1994
### Description
PHP compiles function default arguments to constant ASTs. The AST is evaluated when the function is
called. When the result of the evaluation is a non-refcounted value, PHP assumes that the AST is
pure (will always evaluate to the same value) and will then cache the value and skip re-evaluation
in the next function call. This assumption is incorrect.
```php
<?php
class Foo {
public function __toString() {
static $i = 0;
return (string) $i++;
}
}
function test(string $foo = new Foo() . '') {
var_dump($foo);
}
test();
test();
test();
```
Resulted in this output:
```
string(1) "0"
string(1) "0"
string(1) "0"
```
But I expected this output instead:
```
string(1) "0"
string(1) "1"
string(1) "2"
```
Instead what should be done is evaluation of the AST should report whether the AST was pure or not.
This currently mainly depends on whether
new was used.
### PHP Version
PHP 8.1
### Operating System
_No response_