[php-src] Issue #25415: Segfault in `FilterIterator::next()` after the subclass catches an exception from `parent::__construct()`
| From: | marinelay | Date: | Thu, 12 Nov 2026 10:48:34 +0000 |
| Subject: | [php-src] Issue #25415: Segfault in `FilterIterator::next()` after the subclass catches an exception from `parent::__construct()` | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-254134@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/25415
Author: marinelay
### Description
If a
FilterIterator subclass passes a finished generator to
parent::__construct(), the constructor throws. If the subclass catches that exception,
a later next() call crashes PHP.
The following code:
```php
<?php
class F extends FilterIterator {
public function __construct(Iterator $it) {
try {
parent::__construct($it);
} catch (Exception $e) {
echo "caught: ", $e->getMessage(), "\n";
}
}
public function accept(): bool { return true; }
}
function g() { yield 1; }
$gen = g();
foreach ($gen as $v) {} // the generator is now closed
$f = new F($gen);
$f->next();
echo "done\n";
```
Resulted in this output:
```
caught: Cannot traverse an already closed generator
Segmentation fault (core dumped)
```
With AddressSanitizer (./configure --enable-debug --enable-address-sanitizer), it
reports:
```
caught: Cannot traverse an already closed generator
==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000048 (pc ... T0)
==The signal is caused by a READ memory access.
#0 in spl_dual_it_next ext/spl/spl_iterators.c:1523
#1 in spl_filter_it_next ext/spl/spl_iterators.c:1626
#2 in zim_FilterIterator_next ext/spl/spl_iterators.c:1649
SUMMARY: AddressSanitizer: SEGV ext/spl/spl_iterators.c:1523 in spl_dual_it_next
```
## Description
https://github.com/php/php-src/blob/b8499682c360b2262ffe8581168c24eac5645d74/ext/spl/spl_iterators.c#L1515-L1525
1. parent::__construct() marks the object as constructed at [line
1405](https://github.com/php/php-src/blob/b8499682c360b2262ffe8581168c24eac5645d74/ext/spl/spl_iterators.c#L1405),
then asks the Generator for its iterator at [line
1413](https://github.com/php/php-src/blob/b8499682c360b2262ffe8581168c24eac5645d74/ext/spl/spl_iterators.c#L1413).
A closed Generator [throws and returns
NULL](https://github.com/php/php-src/blob/b8499682c360b2262ffe8581168c24eac5645d74/Zend/zend_generators.c#L1226-L1229),
so the inner iterator stays NULL. The subclass catches the exception.
2. FilterIterator::next() checks only that mark
([SPL_FETCH_AND_CHECK_DUAL_IT](https://github.com/php/php-src/blob/b8499682c360b2262ffe8581168c24eac5645d74/ext/spl/spl_iterators.c#L147-L155)),
then calls spl_dual_it_next() with do_free = 1. The NULL check at line
1519 runs only when do_free is 0, so it is skipped.
3. Line 1523 then uses the NULL inner iterator.
So next() on such an object crashes.
Subclasses of IteratorIterator, LimitIterator,
InfiniteIterator and NoRewindIterator that catch the same exception also
crash when used (I checked these too).
I found this with the Infer static analyzer (Pulse), reproduced it with AddressSanitizer, and used
Claude Opus 5.5 to help read the code and write this issue.
### PHP Version
```plain
PHP 8.7.0-dev (cli) (built: Nov 12 2026 07:37:43) (NTS)
Copyright © The PHP Group and Contributors
Zend Engine v4.7.0-dev, Copyright © Zend by Perforce
with Zend OPcache v8.7.0-dev, Copyright ©, by Zend by Perforce
```
### Operating System
Debian GNU/Linux 13 (trixie), x86_64