Skip to content

Commit 99691b4

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix uncatchable exception thrown in generator
2 parents 5c0a6fe + acff8f0 commit 99691b4

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PHP NEWS
77
with dynamic class const lookup default argument). (ilutov)
88
. Fixed bug GH-20695 (Assertion failure in normalize_value() when parsing
99
malformed INI input via parse_ini_string()). (ndossche)
10+
. Fixed bug GH-20714 (Uncatchable exception thrown in generator). (ilutov)
1011

1112
- EXIF:
1213
. Fixed bug GH-20631 (Integer underflow in exif HEIF parsing

Zend/tests/gh20714.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GH-20714: Uncatchable exception thrown in generator
3+
--CREDITS--
4+
Grégoire Paris (greg0ire)
5+
--FILE--
6+
<?php
7+
8+
function gen(): Generator {
9+
try {
10+
yield 1;
11+
} finally {}
12+
}
13+
14+
function process(): void {
15+
$g = gen();
16+
foreach ($g as $_) {
17+
throw new Exception('ERROR');
18+
}
19+
}
20+
21+
try {
22+
process();
23+
} catch (Exception $e) {
24+
echo "Caught\n";
25+
}
26+
27+
?>
28+
--EXPECT--
29+
Caught

Zend/zend_generators.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
313313
zend_object *old_exception = NULL;
314314
const zend_op *old_opline_before_exception = NULL;
315315
if (EG(exception)) {
316-
if (EG(current_execute_data)) {
316+
if (EG(current_execute_data)
317+
&& EG(current_execute_data)->opline
318+
&& EG(current_execute_data)->opline->opcode == ZEND_HANDLE_EXCEPTION) {
317319
EG(current_execute_data)->opline = EG(opline_before_exception);
318320
old_opline_before_exception = EG(opline_before_exception);
319321
}
@@ -330,7 +332,7 @@ static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
330332
zend_generator_resume(generator);
331333

332334
if (old_exception) {
333-
if (EG(current_execute_data)) {
335+
if (old_opline_before_exception) {
334336
EG(current_execute_data)->opline = EG(exception_op);
335337
EG(opline_before_exception) = old_opline_before_exception;
336338
}

0 commit comments

Comments
 (0)