Skip to content

Fix GH-11591: AppendIterator fails to iterate with an empty generator #17316

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,6 @@ ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
}
/* }}} */

static inline void zend_generator_ensure_initialized(zend_generator *generator) /* {{{ */
{
if (UNEXPECTED(Z_TYPE(generator->value) == IS_UNDEF) && EXPECTED(generator->execute_data) && EXPECTED(generator->node.parent == NULL)) {
zend_generator_resume(generator);
generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
}
}
/* }}} */

static inline void zend_generator_rewind(zend_generator *generator) /* {{{ */
{
zend_generator_ensure_initialized(generator);
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend_generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ static const uint8_t ZEND_GENERATOR_IN_FIBER = 0x10;
void zend_register_generator_ce(void);
ZEND_API void zend_generator_close(zend_generator *generator, bool finished_execution);
ZEND_API void zend_generator_resume(zend_generator *generator);
static inline void zend_generator_ensure_initialized(zend_generator *generator) /* {{{ */
{
if (UNEXPECTED(Z_TYPE(generator->value) == IS_UNDEF) && EXPECTED(generator->execute_data) && EXPECTED(generator->node.parent == NULL)) {
zend_generator_resume(generator);
generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
}
}
/* }}} */
Comment on lines +108 to +115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like exposing this in a header.

I would rather prefer creating a new API (e.g. zend_generator_is_empty), but considering you are trying to fix a bug in SPL it seems far more likely that SPL is doing something incorrect in the iterator code.


ZEND_API void zend_generator_restore_call_stack(zend_generator *generator);
ZEND_API zend_execute_data* zend_generator_freeze_call_stack(zend_execute_data *execute_data);
Expand Down
10 changes: 10 additions & 0 deletions ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "php.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_generators.h"
#include "ext/pcre/php_pcre.h"

#include "spl_iterators.h"
Expand Down Expand Up @@ -2814,6 +2815,15 @@ PHP_METHOD(AppendIterator, append)
RETURN_THROWS();
}

if (instanceof_function(Z_OBJCE_P(it), zend_ce_generator)) {
zend_generator *gen = (zend_generator*)Z_OBJ_P(it);
zend_generator_ensure_initialized(gen);
if (gen->execute_data == NULL) {
/* Skip an empty generator */
return;
}
}

SPL_FETCH_AND_CHECK_DUAL_IT(intern, ZEND_THIS);

if (intern->u.append.iterator->funcs->valid(intern->u.append.iterator) == SUCCESS && spl_dual_it_valid(intern) != SUCCESS) {
Expand Down
17 changes: 17 additions & 0 deletions ext/spl/tests/gh11591.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Bug GH-11591 (Inconsistent behaviour with AppendIterator and empty generators)
--FILE--
<?php
function yieldEmpty()
{
yield from [];
}

$iterator = new AppendIterator();
$iterator->append(yieldEmpty());

foreach ($iterator as $item) {
var_dump($item);
}
?>
--EXPECT--
Loading