-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
Description
I have investigated some weird behaviour and I came to a conclusion it must be an issue in php-src.
Steps to reproduce:
- clone https://github.com/mvorisek/PHP-CS-Fixer/tree/fix_tokens_cache_8228
- run
composer update
- run
php repro.php
- notice output "BUG DETECTED!!"
In the repro branch, there are 3 important files:
- the
repro.php
file that can reproduce the issue consistently, - https://github.com/mvorisek/PHP-CS-Fixer/blob/cbfd08b544/.github/workflows/ci.yml which reproduces the issue in CI,
- and https://github.com/mvorisek/PHP-CS-Fixer/blob/cbfd08b544/src/Tokenizer/Tokens.php#L46
The last file/line is very interesting. How can foreach ($this as $t) {}
have an effect on the result?
a) I know, the $t
is kept allocated until out of scope, but that is not decisive here (tested)
b) the $this
is a class that extends \SplFixedArray
, but the getIterator()
method is not extended (nor getSize()
, count()
, offsetGet()
), so the foreach should have zero effect
The foreach ($this as $t) {}
however fix the corruption! So I guess there is something wrong in php-src internally.
For unknown reasons, the "foreach fix" is working on PHP 7.4 only. I know PHP 7.4 is EOL, but can someone please reproduce the issue on PHP 7.4 and answer why foreach ($this as $t) {}
is fixing the problem, ie. what internal/php-src side effects it does?
I guess with this question answered, it would be easy to understand this main issue even for higher/latest php version(s).
PHP Version
any
Operating System
any (tested Windows and linux)