diff --git a/ext/spl/spl_iterators.c b/ext/spl/spl_iterators.c index 05bdfdb34ba3..6eb2c6ad3807 100644 --- a/ext/spl/spl_iterators.c +++ b/ext/spl/spl_iterators.c @@ -154,7 +154,7 @@ static inline spl_dual_it_object *spl_dual_it_from_obj(zend_object *obj) /* {{{ #define SPL_FETCH_AND_CHECK_DUAL_IT(var, objzval) \ do { \ spl_dual_it_object *it = Z_SPLDUAL_IT_P(objzval); \ - if (it->dit_type == DIT_Unknown) { \ + if (it->dit_type == DIT_Unknown || (!it->inner.iterator && it->dit_type != DIT_AppendIterator)) { \ zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \ RETURN_THROWS(); \ } \ diff --git a/ext/spl/tests/gh25415.phpt b/ext/spl/tests/gh25415.phpt new file mode 100644 index 000000000000..036cdeb504fc --- /dev/null +++ b/ext/spl/tests/gh25415.phpt @@ -0,0 +1,70 @@ +--TEST-- +GH-25415 (Segfault in FilterIterator::next() after the subclass catches an exception from parent::__construct()) +--CREDITS-- +marinelay +--FILE-- +getMessage(), "\n"; + } + } + public function accept(): bool { return true; } + }, + new class($gen) extends IteratorIterator { + public function __construct(Iterator $it) { + try { + parent::__construct($it); + } catch (Exception $e) { + echo $e->getMessage(), "\n"; + } + } + }, + new class($gen) extends NoRewindIterator { + public function __construct(Iterator $it) { + try { + parent::__construct($it); + } catch (Exception $e) { + echo $e->getMessage(), "\n"; + } + } + }, +]; + +foreach ($iterators as $it) { + foreach (['valid', 'key', 'current', 'next'] as $method) { + try { + $it->$method(); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + } +} + +?> +--EXPECT-- +Cannot traverse an already closed generator +Cannot traverse an already closed generator +Cannot traverse an already closed generator +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called +The object is in an invalid state as the parent constructor was not called