Skip to content
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
30 changes: 30 additions & 0 deletions Zend/tests/debug_backtrace_generator_placeholder.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
debug_backtrace skip_last into generator place holder frames
--FILE--
<?php
function Generate() {
yield from [debug_backtrace(skip_last: 0),
debug_backtrace(skip_last: 1),
debug_backtrace(skip_last: 2)];
}

foreach(Generate() as $trace) {
if (!$trace) {
printf("empty\n\n");
continue;
}

foreach ($trace as $frame) {
printf("%s in %s on line %d\n", $frame["function"], $frame["file"], $frame["line"]);
}
echo PHP_EOL;
}
?>
--EXPECTF--
debug_backtrace in %s on line 3
Generate in %s on line 8

Generate in %s on line 8

empty

12 changes: 12 additions & 0 deletions Zend/tests/debug_backtrace_skip_last_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
debug_backtrace skip_last error
--FILE--
<?php
try {
debug_backtrace(skip_last: -1);
} catch (Error $ex) {
printf("%s\n", $ex->getMessage());
}
?>
--EXPECT--
debug_backtrace(): Argument #3 ($skip_last) must be greater than or equal to zero
12 changes: 12 additions & 0 deletions Zend/tests/debug_print_backtrace_skip_last_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
debug_print_backtrace skip_last error
--FILE--
<?php
try {
debug_print_backtrace(skip_last: -1);
} catch (Error $ex) {
printf("%s\n", $ex->getMessage());
}
?>
--EXPECT--
debug_print_backtrace(): Argument #3 ($skip_last) must be greater than or equal to zero
30 changes: 24 additions & 6 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,11 @@ ZEND_FUNCTION(debug_print_backtrace)
RETURN_THROWS();
}

if (skip_last < 0) {
zend_argument_value_error(3, "must be greater than or equal to zero");
RETURN_THROWS();
}

zend_fetch_debug_backtrace(&backtrace, skip_last, options, limit);
ZEND_ASSERT(Z_TYPE(backtrace) == IS_ARRAY);

Expand Down Expand Up @@ -1685,13 +1690,21 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
return;
}

while (skip_last--) {
if (!call->prev_execute_data) {
break;
}

if (skip_last == 1) {
call = call->prev_execute_data;
}
} else if (skip_last > 1) {
while (skip_last--) {
if (!call->prev_execute_data) {
break;
}

if (ZEND_CALL_INFO(call) & ZEND_CALL_GENERATOR) {
call = zend_generator_check_placeholder_frame(call->prev_execute_data);
} else {
call = call->prev_execute_data;
}
}
}

while (call && (limit == 0 || frameno < limit)) {
zend_execute_data *prev = call->prev_execute_data;
Expand Down Expand Up @@ -1879,6 +1892,11 @@ ZEND_FUNCTION(debug_backtrace)
RETURN_THROWS();
}

if (skip_last < 0) {
zend_argument_value_error(3, "must be greater than or equal to zero");
RETURN_THROWS();
}

zend_fetch_debug_backtrace(return_value, skip_last, options, limit);
}
/* }}} */
Expand Down