Skip to content

Commit a35e616

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix uncatchable exception thrown in generator
2 parents 122a94e + 99691b4 commit a35e616

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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
@@ -312,7 +312,9 @@ static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
312312
zend_object *old_exception = NULL;
313313
const zend_op *old_opline_before_exception = NULL;
314314
if (EG(exception)) {
315-
if (EG(current_execute_data)) {
315+
if (EG(current_execute_data)
316+
&& EG(current_execute_data)->opline
317+
&& EG(current_execute_data)->opline->opcode == ZEND_HANDLE_EXCEPTION) {
316318
EG(current_execute_data)->opline = EG(opline_before_exception);
317319
old_opline_before_exception = EG(opline_before_exception);
318320
}
@@ -329,7 +331,7 @@ static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
329331
zend_generator_resume(generator);
330332

331333
if (old_exception) {
332-
if (EG(current_execute_data)) {
334+
if (old_opline_before_exception) {
333335
EG(current_execute_data)->opline = EG(exception_op);
334336
EG(opline_before_exception) = old_opline_before_exception;
335337
}

0 commit comments

Comments
 (0)