Skip to content

Commit

Permalink
Fix phpGH-14387: Crash when stack walking in destructor of yielded fr…
Browse files Browse the repository at this point in the history
…om values during Generator->throw()
  • Loading branch information
bwoebi committed Jun 1, 2024
1 parent 18233e0 commit be7f3aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ PHP NEWS
. Fixed bug GH-14315 (Incompatible pointer type warnings). (Peter Kokot)
. Fixed bug GH-12814 (max_execution_time reached too early on MacOS 14
when running on Apple Silicon). (Manuel Kress)
. Fixed bug GH-14387 (Crash when stack walking in destructor of yielded from
values during Generator->throw()). (Bob)

- BCMatch:
. Fixed bug (bcpowmod() with mod = -1 returns 1 when it must be 0). (Girgias)
Expand Down
35 changes: 35 additions & 0 deletions Zend/tests/generators/gh14387.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
GH-14387 (Crash when stack walking in destructor of yielded from values during Generator->throw())
--FILE--
<?php

function prime(Generator $generator) {
$generator->valid();
}

$g = (function () {
yield from [null, new class {
function __destruct() {
// Trigger a stack walk, hitting a bad frame.
throw new Exception;
}
}];
})();

prime($g);

$g->throw(new Error);

?>
--EXPECTF--
Fatal error: Uncaught Error in %s:%d
Stack trace:
#0 {main}

Next Exception in %s:%d
Stack trace:
#0 %s(%d): class@anonymous->__destruct()
#1 [internal function]: {%s}()
#2 %s(%d): Generator->throw(Object(Error))
#3 {main}
thrown in %s on line %d
1 change: 1 addition & 0 deletions Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ static void zend_generator_throw_exception(zend_generator *generator, zval *exce
* to pretend the exception happened during the YIELD opcode. */
EG(current_execute_data) = generator->execute_data;
generator->execute_data->opline--;
generator->execute_data->prev_execute_data = original_execute_data;

if (exception) {
zend_throw_exception_object(exception);
Expand Down

0 comments on commit be7f3aa

Please sign in to comment.