Skip to content

Commit 659f55a

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Prevent throwing in running generator
2 parents 9db21e1 + 781d77a commit 659f55a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
(Arnaud)
1414
. Fixed bug GH-19306 (Generator can be resumed while fetching next value from
1515
delegated Generator). (Arnaud)
16+
. Fixed bug GH-19326 (Calling Generator::throw() on a running generator with
17+
a non-Generator delegate crashes). (Arnaud)
1618

1719
- Curl:
1820
. Add support for CURLINFO_CONN_ID in curl_getinfo() (thecaliskan)

Zend/tests/gh19326.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-19326: Calling Generator::throw() on a running generator with a non-Generator delegate crashes
3+
--FILE--
4+
<?php
5+
6+
class It implements IteratorAggregate {
7+
public function getIterator(): Generator {
8+
yield "";
9+
Fiber::suspend();
10+
}
11+
}
12+
13+
function g() {
14+
yield from new It();
15+
}
16+
17+
$b = g();
18+
$b->rewind();
19+
20+
$fiber = new Fiber(function () use ($b) {
21+
$b->next();
22+
});
23+
24+
$fiber->start();
25+
26+
try {
27+
$b->throw(new Exception('test'));
28+
} catch (Error $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
$fiber->resume();
33+
34+
?>
35+
--EXPECT--
36+
Cannot resume an already running generator

Zend/zend_generators.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,14 @@ ZEND_API zend_execute_data *zend_generator_check_placeholder_frame(zend_execute_
498498
return ptr;
499499
}
500500

501-
static void zend_generator_throw_exception(zend_generator *generator, zval *exception)
501+
static zend_result zend_generator_throw_exception(zend_generator *generator, zval *exception)
502502
{
503+
if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
504+
zval_ptr_dtor(exception);
505+
zend_throw_error(NULL, "Cannot resume an already running generator");
506+
return FAILURE;
507+
}
508+
503509
zend_execute_data *original_execute_data = EG(current_execute_data);
504510

505511
/* Throw the exception in the context of the generator. Decrementing the opline
@@ -520,6 +526,8 @@ static void zend_generator_throw_exception(zend_generator *generator, zval *exce
520526
}
521527

522528
EG(current_execute_data) = original_execute_data;
529+
530+
return SUCCESS;
523531
}
524532

525533
static void zend_generator_add_child(zend_generator *generator, zend_generator *child)
@@ -1026,7 +1034,9 @@ ZEND_METHOD(Generator, throw)
10261034
if (generator->execute_data) {
10271035
zend_generator *root = zend_generator_get_current(generator);
10281036

1029-
zend_generator_throw_exception(root, exception);
1037+
if (zend_generator_throw_exception(root, exception) == FAILURE) {
1038+
return;
1039+
}
10301040

10311041
zend_generator_resume(generator);
10321042

0 commit comments

Comments
 (0)