Skip to content

Commit e25aab6

Browse files
committed
Fixed bug #79927
We need to unset the AT_FIRST_YIELD flag when yielding from an array as well. In the interest of being conservative, I'm applying this only to PHP 8.
1 parent 227f1f1 commit e25aab6

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ PHP NEWS
2121
exit value in CLI). (Nikita)
2222
. Fixed bug #62294 (register_shutdown_function() does not correctly handle
2323
exit code). (Nikita)
24+
. Fixed bug #79927 (Generator doesn't throw exception after multiple yield
25+
from iterable). (Nikita)
2426

2527
- Date:
2628
. Fixed bug #60302 (DateTime::createFromFormat should new static(), not new

Zend/tests/bug79927.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Bug #79927: Generator doesn't throw exception after multiple yield from iterable
3+
--FILE--
4+
<?php
5+
6+
$generator = (function () {
7+
yield from [1, 2, 3];
8+
})();
9+
10+
$generator->next();
11+
$generator->next();
12+
try {
13+
$generator->rewind();
14+
} catch (Exception $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
echo $generator->current(), "\n";
18+
19+
$generator2 = (function () {
20+
yield from [];
21+
yield 4;
22+
})();
23+
$generator2->current();
24+
$generator2->rewind();
25+
echo $generator2->current(), "\n";
26+
27+
?>
28+
--EXPECT--
29+
Cannot rewind a generator that was already run
30+
3
31+
4

Zend/zend_generators.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
731731
return;
732732
}
733733

734+
/* Drop the AT_FIRST_YIELD flag */
735+
orig_generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
736+
734737
if (UNEXPECTED(!Z_ISUNDEF(generator->values))) {
735738
if (EXPECTED(zend_generator_get_next_delegated_value(generator) == SUCCESS)) {
736739
orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
@@ -740,9 +743,6 @@ ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
740743
* after the "yield from" expression. */
741744
}
742745

743-
/* Drop the AT_FIRST_YIELD flag */
744-
orig_generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
745-
746746
{
747747
/* Backup executor globals */
748748
zend_execute_data *original_execute_data = EG(current_execute_data);

0 commit comments

Comments
 (0)