Skip to content

Fix access on NULL when printing backtrace with freed generator #15952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Zend/tests/generators/gh15851.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-15851: Access on NULL when printing backtrace with freed generator
--FILE--
<?php

class Foo {
public function __destruct() {
debug_print_backtrace();
}
}

function bar() {
yield from foo();
}

function foo() {
$foo = new Foo();
yield;
}

$gen = bar();
foreach ($gen as $dummy);

?>
--EXPECTF--
#0 %s(%d): Foo->__destruct()
#1 %s(%d): bar()
10 changes: 10 additions & 0 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,16 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
}

while (call && (limit == 0 || frameno < limit)) {
if (UNEXPECTED(!call->func)) {
/* This is the fake frame inserted for nested generators. Normally,
* this frame is preceded by the actual generator frame and then
* replaced by zend_generator_check_placeholder_frame() below.
* However, the frame is popped before cleaning the stack frame,
* which is observable by destructors. */
call = zend_generator_check_placeholder_frame(call);
ZEND_ASSERT(call->func);
}

zend_execute_data *prev = call->prev_execute_data;

if (!prev) {
Expand Down
1 change: 1 addition & 0 deletions sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
object_init_ex(&ref, pce);

memset(&execute_data, 0, sizeof(zend_execute_data));
execute_data.func = (zend_function *) &zend_pass_function;
EG(current_execute_data) = &execute_data;
zend_call_known_instance_method_with_1_params(
pce->constructor, Z_OBJ(ref), NULL, &arg);
Expand Down
Loading