Skip to content

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

Closed
wants to merge 1 commit 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
21 changes: 21 additions & 0 deletions Zend/tests/gh7922.phpt
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
37 changes: 20 additions & 17 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Contributor

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.

zval_ptr_dtor(&retval);
/* fallthrough */
case IS_NULL:
*is_temp = 1;
ht = Z_ARR(retval);
ht = zend_new_array(0);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 zend_empty_array (which would be non-reference-counted and have to have *is_temp = 0)

#define ZVAL_EMPTY_ARRAY(z) do {						\
		zval *__z = (z);								\
		Z_ARR_P(__z) = (zend_array*)&zend_empty_array;	\
		Z_TYPE_INFO_P(__z) = IS_ARRAY; \
	} while (0)

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... */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly no longer needed if switch is comprehensive, haven't tested.

}
/* }}} */
Expand Down