1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Partially fix GH-16317: SimpleXML does not allow __debugInfo() overrides to work

If only we did not have the pseudo-key "@attributes", we could've just
removed the custom get_debug_info implementation and this would work out
of the box. Anyway, we just have to manually check for an override now.

Closes GH-20131.
This commit is contained in:
Niels Dossche
2025-10-11 12:03:11 +02:00
parent 9216b8be8b
commit efa1fafc2f
5 changed files with 38 additions and 6 deletions

4
NEWS
View File

@@ -22,6 +22,10 @@ PHP NEWS
- Random:
. Fix Randomizer::__serialize() w.r.t. INDIRECTs. (nielsdos)
- SimpleXML:
. Partially fixed bug GH-16317 (SimpleXML does not allow __debugInfo() overrides
to work). (nielsdos)
- XMLReader:
. Fix arginfo/zpp violations when LIBXML_SCHEMAS_ENABLED is not available.
(nielsdos)

View File

@@ -1225,12 +1225,12 @@ static HashTable *sxe_get_properties(zend_object *object) /* {{{ */
}
/* }}} */
static HashTable * sxe_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
/* This custom handler exists because the var_dump adds a pseudo "@attributes" key. */
PHP_METHOD(SimpleXMLElement, __debugInfo)
{
*is_temp = 1;
return sxe_get_prop_hash(object, 1);
ZEND_PARSE_PARAMETERS_NONE();
RETURN_ARR(sxe_get_prop_hash(Z_OBJ_P(ZEND_THIS), 1));
}
/* }}} */
static int sxe_objects_compare(zval *object1, zval *object2) /* {{{ */
{
@@ -2733,7 +2733,6 @@ PHP_MINIT_FUNCTION(simplexml)
sxe_object_handlers.compare = sxe_objects_compare;
sxe_object_handlers.cast_object = sxe_object_cast;
sxe_object_handlers.count_elements = sxe_count_elements;
sxe_object_handlers.get_debug_info = sxe_get_debug_info;
sxe_object_handlers.get_closure = NULL;
sxe_object_handlers.get_gc = sxe_get_gc;

View File

@@ -51,6 +51,8 @@ class SimpleXMLElement implements Stringable, Countable, RecursiveIterator
public function __toString(): string {}
public function __debugInfo(): ?array {}
/** @tentative-return-type */
public function count(): int {}

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 06c88dc2fb5582a6d21c11aee6ac0a0538e70cbc */
* Stub hash: 3da8d1222740f381602c0ec738fe6aa9dca56c8e */
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_simplexml_load_file, 0, 1, SimpleXMLElement, MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
@@ -79,6 +79,9 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement___toString, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement___debugInfo, 0, 0, IS_ARRAY, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_SimpleXMLElement_count, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
@@ -116,6 +119,7 @@ ZEND_METHOD(SimpleXMLElement, addChild);
ZEND_METHOD(SimpleXMLElement, addAttribute);
ZEND_METHOD(SimpleXMLElement, getName);
ZEND_METHOD(SimpleXMLElement, __toString);
ZEND_METHOD(SimpleXMLElement, __debugInfo);
ZEND_METHOD(SimpleXMLElement, count);
ZEND_METHOD(SimpleXMLElement, rewind);
ZEND_METHOD(SimpleXMLElement, valid);
@@ -148,6 +152,7 @@ static const zend_function_entry class_SimpleXMLElement_methods[] = {
ZEND_ME(SimpleXMLElement, addAttribute, arginfo_class_SimpleXMLElement_addAttribute, ZEND_ACC_PUBLIC)
ZEND_ME(SimpleXMLElement, getName, arginfo_class_SimpleXMLElement_getName, ZEND_ACC_PUBLIC)
ZEND_ME(SimpleXMLElement, __toString, arginfo_class_SimpleXMLElement___toString, ZEND_ACC_PUBLIC)
ZEND_ME(SimpleXMLElement, __debugInfo, arginfo_class_SimpleXMLElement___debugInfo, ZEND_ACC_PUBLIC)
ZEND_ME(SimpleXMLElement, count, arginfo_class_SimpleXMLElement_count, ZEND_ACC_PUBLIC)
ZEND_ME(SimpleXMLElement, rewind, arginfo_class_SimpleXMLElement_rewind, ZEND_ACC_PUBLIC)
ZEND_ME(SimpleXMLElement, valid, arginfo_class_SimpleXMLElement_valid, ZEND_ACC_PUBLIC)

View File

@@ -0,0 +1,22 @@
--TEST--
GH-16317 (SimpleXML does not allow __debugInfo() overrides to work)
--FILE--
<?php
class MySXE extends SimpleXMLElement {
public function __debugInfo(): array {
echo "invoked\n";
return ['x' => 1];
}
}
$sxe = simplexml_load_string('<root><a/></root>', MySXE::class);
var_dump($sxe);
?>
--EXPECT--
invoked
object(MySXE)#1 (1) {
["x"]=>
int(1)
}