-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement GH-7922: print_r should not produce a fatal error #7964
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
GH-7922 (print_r should not produce a fatal error) | ||
--FILE-- | ||
<?php | ||
class A | ||
{ | ||
public function __debugInfo(): array | ||
{ | ||
throw new \Error('x'); | ||
} | ||
} | ||
|
||
try { | ||
$a = new A(); | ||
print_r($a, true); | ||
} catch (\Throwable $e) { | ||
echo $e->getMessage(); | ||
} | ||
?> | ||
--EXPECT-- | ||
__debuginfo() must return an array |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,27 +160,30 @@ ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) / | |
} | ||
|
||
zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval); | ||
if (Z_TYPE(retval) == IS_ARRAY) { | ||
if (!Z_REFCOUNTED(retval)) { | ||
*is_temp = 1; | ||
return zend_array_dup(Z_ARRVAL(retval)); | ||
} else if (Z_REFCOUNT(retval) <= 1) { | ||
switch (Z_TYPE(retval)) { | ||
case IS_ARRAY: | ||
if (!Z_REFCOUNTED(retval)) { | ||
*is_temp = 1; | ||
return zend_array_dup(Z_ARRVAL(retval)); | ||
} else if (Z_REFCOUNT(retval) <= 1) { | ||
*is_temp = 1; | ||
ht = Z_ARR(retval); | ||
return ht; | ||
} else { | ||
*is_temp = 0; | ||
zval_ptr_dtor(&retval); | ||
return Z_ARRVAL(retval); | ||
} | ||
default: | ||
zend_throw_error(NULL, ZEND_DEBUGINFO_FUNC_NAME "() must return an array"); | ||
zval_ptr_dtor(&retval); | ||
/* fallthrough */ | ||
case IS_NULL: | ||
*is_temp = 1; | ||
ht = Z_ARR(retval); | ||
ht = zend_new_array(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not familiar enough with this code to be sure: Does this code predate
|
||
return ht; | ||
} else { | ||
*is_temp = 0; | ||
zval_ptr_dtor(&retval); | ||
return Z_ARRVAL(retval); | ||
} | ||
} else if (Z_TYPE(retval) == IS_NULL) { | ||
*is_temp = 1; | ||
ht = zend_new_array(0); | ||
return ht; | ||
} | ||
|
||
zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array"); | ||
|
||
return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly no longer needed if switch is comprehensive, haven't tested. |
||
} | ||
/* }}} */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be thrown if
EG(exception)
is already non-null after the call? It seems more intuitive (for userland expectations) to have the original exception than to replace it.