-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
iterator_count runs indefinitely if iterator doesn't complete #8235
Comments
Normally this sort of "PHP code runs for a long time without running out of memory due to some infinite loop" problem is covered by the |
I think we need to bail out in case of overflow: php-src/ext/spl/spl_iterators.c Line 3130 in 0ba9216
While that may not happen in practice, wrapping around certainly makes no sense. |
We need to prevent integer overflow to eventually stop the iteration. A test case doesn't appear sensible for this, because even on 32bit architectures a respective test easily runs for a few minutes.
* PHP-8.0: Fix GH-8235: iterator_count() may run indefinitely
* PHP-8.1: Fix GH-8235: iterator_count() may run indefinitely
Description
The following code:
Resulted in this output:
But I expected this output instead:
When an iterator never finishes, the
iterator_count
method simply hangs. This is in contrast to theiterator_to_array
method, which throws a fatal error when it runs out of memory. Sinceiterator_count
will continue running, and eventually overflow, no exception is thrown.This is obviously a tricky situation. But it would be useful if there were some way to detect that an iterator is unlikely to finish, and throw an exception of some sort, rather than simply allowing the program to hang.
I've observed this behavior in multiple versions of PHP (8.0+), as well as multiple operating systems (desktop Linux, Android, and macOS)
PHP Version
8.1.3
Operating System
Darwin 21.3.0
The text was updated successfully, but these errors were encountered: