[php-src] Issue #8235: iterator_count runs indefinitely if iterator doesn't complete
| From: | goodevilgenius | Date: | Mon, 21 Mar 2022 16:29:51 +0000 |
| Subject: | [php-src] Issue #8235: iterator_count runs indefinitely if iterator doesn't complete | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-240442@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8235
Author: goodevilgenius
### Description
The following code:
```php
<?php
function fibonacci(): Generator
{
yield $a = 1;
yield $b = 2;
start:
yield $c = $a + $b;
$a = $b;
$b = $c;
goto start;
}
$gen = fibonacci();
iterator_count($gen);
```
Resulted in this output:
```
// None. Program stalls
```
But I expected this output instead:
```
// some sort of exception
```
When an iterator never finishes, the
iterator_count method simply hangs. This is in
contrast to the iterator_to_array method, which throws a fatal error when it runs out
of memory. Since iterator_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