Skip to content

Commit 509af2d

Browse files
iluuu1994mvorisek
authored andcommitted
Stop execution of print_r on error
1 parent 6622d98 commit 509af2d

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

Zend/tests/debug_info-error-0.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $c = new C(0);
1717
var_dump($c);
1818
?>
1919
--EXPECTF--
20-
Fatal error: Uncaught Error: __debuginfo() must return an array in __debuginfo() must return an array in %s%eZend%etests%edebug_info-error-0.php:14
20+
Fatal error: Uncaught Error: __debuginfo() must return an array in %s:%d
2121
Stack trace:
2222
#0 %s(14): var_dump(Object(C))
2323
#1 {main}

Zend/tests/gh7922.phpt

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,90 @@ class A
1010
}
1111
}
1212

13+
echo "Exception in __debugInfo:\n";
14+
1315
try {
14-
$a = new A();
15-
print_r($a);
16+
print_r(new A());
1617
} catch (\Throwable $e) {
17-
echo (string) $e . "\n";
18+
echo $e . "\n";
1819
}
1920

21+
echo "\nException in __debugInfo in anonymous class:\n";
22+
2023
try {
21-
$a = new class() extends A {
24+
print_r(new class() extends A {
2225
public function __debugInfo(): array
2326
{
2427
throw new \Exception('y');
2528
}
26-
};
27-
print_r($a, true);
29+
});
2830
} catch (\Throwable $e) {
29-
echo (string) $e . "\n";
31+
echo $e . "\n";
3032
}
3133

34+
echo "\nException in __destruct of __debugInfo in anonymous class:\n";
35+
3236
try {
33-
$a = new class() extends A {
37+
print_r(new class() extends A {
3438
public function __debugInfo(): array
3539
{
36-
new class() {
40+
$o = new class() {
3741
public function __destruct()
3842
{
39-
throw new \Exception('z_wo_assign');
43+
throw new \Exception('z_w_assign');
4044
}
4145
};
4246

4347
return ['foo' => 2];
4448
}
45-
};
46-
print_r($a);
49+
});
4750
} catch (\Throwable $e) {
48-
echo (string) $e . "\n";
51+
echo $e . "\n";
4952
}
5053

51-
try {
52-
$a = new class() extends A {
53-
public function __debugInfo(): array
54-
{
55-
$o = new class() {
56-
public function __destruct()
57-
{
58-
throw new \Exception('z_w_assign');
59-
}
60-
};
54+
echo "\nException occuring later on:\n";
6155

62-
return ['foo' => 2];
63-
}
64-
};
65-
print_r($a);
56+
try {
57+
print_r(['foo' => 'bar', 'baz' => new A()]);
6658
} catch (\Throwable $e) {
67-
echo (string) $e . "\n";
59+
echo $e . "\n";
6860
}
61+
6962
?>
7063
--EXPECTF--
71-
Error: x in %s:6
64+
Exception in __debugInfo:
65+
A Object
66+
Error: x in %s:%d
7267
Stack trace:
7368
#0 [internal function]: A->__debugInfo()
74-
#1 %s(12): print_r(Object(A))
69+
#1 %s(%d): print_r(Object(A))
7570
#2 {main}
7671

77-
Exception: y in %s:21
72+
Exception in __debugInfo in anonymous class:
73+
A@anonymous Object
74+
Exception: y in %s:%d
7875
Stack trace:
7976
#0 [internal function]: A@anonymous->__debugInfo()
80-
#1 %s(24): print_r(Object(A@anonymous), true)
77+
#1 %s(%d): print_r(Object(A@anonymous))
8178
#2 {main}
8279

83-
Exception: z_wo_assign in %s:35
80+
Exception in __destruct of __debugInfo in anonymous class:
81+
A@anonymous Object
82+
Exception: z_w_assign in %s:%d
8483
Stack trace:
85-
#0 [internal function]: A@anonymous->__destruct()
86-
#1 %s(42): print_r(Object(A@anonymous))
84+
#0 [internal function]: class@anonymous->__destruct()
85+
#1 %s(%d): print_r(Object(A@anonymous))
8786
#2 {main}
8887

89-
Exception: z_w_assign in %s:54
88+
Exception occuring later on:
89+
Array
90+
(
91+
[foo] => bar
92+
[baz] => A Object
93+
94+
)
95+
Error: x in %s:%d
9096
Stack trace:
91-
#0 [internal function]: A@anonymous->__destruct()
92-
#1 %s(61): print_r(Object(A@anonymous))
97+
#0 [internal function]: A->__debugInfo()
98+
#1 %s(%d): print_r(Array)
9399
#2 {main}

Zend/zend_object_handlers.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /
164164
}
165165

166166
zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval);
167+
if (EG(exception)) {
168+
zval_ptr_dtor(&retval);
169+
return NULL;
170+
}
171+
167172
switch (Z_TYPE(retval)) {
168173
case IS_ARRAY:
169174
if (!Z_REFCOUNTED(retval)) {
@@ -178,14 +183,14 @@ ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /
178183
zval_ptr_dtor(&retval);
179184
return Z_ARRVAL(retval);
180185
}
181-
default:
182-
zend_throw_error(NULL, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
183-
zval_ptr_dtor(&retval);
184-
/* fallthrough */
185186
case IS_NULL:
186187
*is_temp = 1;
187188
ht = zend_new_array(0);
188189
return ht;
190+
default:
191+
zend_throw_error(NULL, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
192+
zval_ptr_dtor(&retval);
193+
return NULL;
189194
}
190195

191196
return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */

ext/standard/var.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
161161
Z_PROTECT_RECURSION_P(struc);
162162

163163
myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
164+
if (EG(exception)) {
165+
break;
166+
}
164167
class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
165168
php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0);
166169
zend_string_release_ex(class_name, 0);

0 commit comments

Comments
 (0)