Skip to content

Commit 91ced6a

Browse files
committed
Adds possibility to include called object in exception backtrace
1 parent dcc4b0f commit 91ced6a

File tree

6 files changed

+142
-3
lines changed

6 files changed

+142
-3
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
--TEST--
2+
Exceptions providing object
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public $random;
8+
9+
function __construct() {
10+
$this->random = random_int(0, 100);
11+
}
12+
13+
function doThrow(...$args) {
14+
throw new Exception();
15+
}
16+
}
17+
18+
function doThrow(...$args) {
19+
(new Test())->doThrow(...$args);
20+
}
21+
22+
echo "zend.exception_ignore_args=0\n";
23+
echo "zend.exception_provide_object=1\n";
24+
ini_set("zend.exception_ignore_args", 0);
25+
ini_set("zend.exception_provide_object", 1);
26+
27+
try {
28+
doThrow("foo", "bar");
29+
} catch (Exception $e) {
30+
var_dump($e->getTrace());
31+
}
32+
33+
echo "zend.exception_ignore_args=1\n";
34+
echo "zend.exception_provide_object=0\n";
35+
ini_set("zend.exception_ignore_args", 1);
36+
ini_set("zend.exception_provide_object", 0);
37+
38+
try {
39+
doThrow("foo", "bar");
40+
} catch (Exception $e) {
41+
var_dump($e->getTrace());
42+
}
43+
44+
?>
45+
--EXPECTF--
46+
zend.exception_ignore_args=0
47+
zend.exception_provide_object=1
48+
array(2) {
49+
[0]=>
50+
array(7) {
51+
["file"]=>
52+
string(%d) "%s/exception_provide_object.php"
53+
["line"]=>
54+
int(%d)
55+
["function"]=>
56+
string(7) "doThrow"
57+
["class"]=>
58+
string(4) "Test"
59+
["object"]=>
60+
object(Test)#%d (%d) {
61+
["random"]=>
62+
int(%d)
63+
}
64+
["type"]=>
65+
string(2) "->"
66+
["args"]=>
67+
array(2) {
68+
[0]=>
69+
string(3) "foo"
70+
[1]=>
71+
string(3) "bar"
72+
}
73+
}
74+
[1]=>
75+
array(4) {
76+
["file"]=>
77+
string(%d) "%s/exception_provide_object.php"
78+
["line"]=>
79+
int(25)
80+
["function"]=>
81+
string(7) "doThrow"
82+
["args"]=>
83+
array(2) {
84+
[0]=>
85+
string(3) "foo"
86+
[1]=>
87+
string(3) "bar"
88+
}
89+
}
90+
}
91+
zend.exception_ignore_args=1
92+
zend.exception_provide_object=0
93+
array(2) {
94+
[0]=>
95+
array(5) {
96+
["file"]=>
97+
string(%d) "%s/exception_provide_object.php"
98+
["line"]=>
99+
int(%d)
100+
["function"]=>
101+
string(7) "doThrow"
102+
["class"]=>
103+
string(4) "Test"
104+
["type"]=>
105+
string(2) "->"
106+
}
107+
[1]=>
108+
array(3) {
109+
["file"]=>
110+
string(%d) "%s/exception_provide_object.php"
111+
["line"]=>
112+
int(%d)
113+
["function"]=>
114+
string(7) "doThrow"
115+
}
116+
}

Zend/zend.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ ZEND_INI_BEGIN()
270270
STD_ZEND_INI_BOOLEAN("zend.signal_check", SIGNAL_CHECK_DEFAULT, ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
271271
#endif
272272
STD_ZEND_INI_BOOLEAN("zend.exception_ignore_args", "0", ZEND_INI_ALL, OnUpdateBool, exception_ignore_args, zend_executor_globals, executor_globals)
273+
STD_ZEND_INI_BOOLEAN("zend.exception_provide_object", "0", ZEND_INI_ALL, OnUpdateBool, exception_provide_object, zend_executor_globals, executor_globals)
273274
STD_ZEND_INI_ENTRY("zend.exception_string_param_max_len", "15", ZEND_INI_ALL, OnSetExceptionStringParamMaxLen, exception_string_param_max_len, zend_executor_globals, executor_globals)
274275
STD_ZEND_INI_ENTRY("fiber.stack_size", NULL, ZEND_INI_ALL, OnUpdateFiberStackSize, fiber_stack_size, zend_executor_globals, executor_globals)
275276
#ifdef ZEND_CHECK_STACK_LIMIT

Zend/zend_exceptions.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,19 @@ static zend_object *zend_default_exception_new(zend_class_entry *class_type) /*
290290
zval tmp;
291291
zval trace;
292292
zend_string *filename;
293+
int options = 0;
293294

294295
zend_object *object = zend_objects_new(class_type);
295296
object_properties_init(object, class_type);
296297

297298
if (EG(current_execute_data)) {
298-
zend_fetch_debug_backtrace(&trace,
299-
0,
300-
EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
299+
if (EG(exception_ignore_args)) {
300+
options |= DEBUG_BACKTRACE_IGNORE_ARGS;
301+
}
302+
if (EG(exception_provide_object)) {
303+
options |= DEBUG_BACKTRACE_PROVIDE_OBJECT;
304+
}
305+
zend_fetch_debug_backtrace(&trace, 0, options, 0);
301306
} else {
302307
ZVAL_EMPTY_ARRAY(&trace);
303308
}

Zend/zend_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ struct _zend_executor_globals {
235235

236236
int user_error_handler_error_reporting;
237237
bool exception_ignore_args;
238+
bool exception_provide_object;
238239
zval user_error_handler;
239240
zval user_exception_handler;
240241
zend_stack user_error_handlers_error_reporting;

php.ini-development

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ zend.enable_gc = On
373373
; Production Value: On
374374
zend.exception_ignore_args = Off
375375

376+
; Allows to include or exclude called object from stack traces generated for exceptions.
377+
; In production, it is recommended to turn this setting off to prohibit the output
378+
; of sensitive information in stack traces
379+
; Default Value: Off
380+
; Development Value: On
381+
; Production Value: Off
382+
zend.exception_provide_object = On
383+
376384
; Allows setting the maximum string length in an argument of a stringified stack trace
377385
; to a value between 0 and 1000000.
378386
; This has no effect when zend.exception_ignore_args is enabled.

php.ini-production

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ zend.enable_gc = On
373373
; Production Value: On
374374
zend.exception_ignore_args = On
375375

376+
; Allows to include or exclude called object from stack traces generated for exceptions.
377+
; In production, it is recommended to turn this setting off to prohibit the output
378+
; of sensitive information in stack traces
379+
; Default Value: Off
380+
; Development Value: On
381+
; Production Value: Off
382+
zend.exception_provide_object = Off
383+
376384
; Allows setting the maximum string length in an argument of a stringified stack trace
377385
; to a value between 0 and 1000000.
378386
; This has no effect when zend.exception_ignore_args is enabled.

0 commit comments

Comments
 (0)