Skip to content

Fix GH-12223: Entity reference produces infinite loop in var_dump/print_r #12223

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
6 changes: 6 additions & 0 deletions ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,12 @@ static HashTable *sxe_get_prop_hash(zend_object *object, int is_debug) /* {{{ */
sxe_properties_add(rv, name, namelen, &value);
}
next_iter:
if (UNEXPECTED(node->type == XML_ENTITY_DECL)) {
/* Entity decls are linked together via the next pointer.
* The only way to get to an entity decl is via an entity reference in the document.
* If we then continue iterating, we'll end up in the DTD. Even worse, if the entities reference each other we'll infinite loop. */
break;
}
if (use_iter) {
node = php_sxe_iterator_fetch(sxe, node->next, 0);
} else {
Expand Down
67 changes: 67 additions & 0 deletions ext/simplexml/tests/gh12223.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--TEST--
GH-12223: Entity reference produces infinite loop in var_dump/print_r
--EXTENSIONS--
simplexml
--FILE--
<?php

$xml = <<<XML
<?xml version="1.0"?>
<!DOCTYPE somedoc [
<!ENTITY a "something">
<!ENTITY b "&a;">
<!ENTITY c "&b;">
]>
<somedoc>&c;</somedoc>
XML;

$sxe = simplexml_load_string($xml);

var_dump($sxe);
print_r($sxe);

?>
--EXPECT--
object(SimpleXMLElement)#1 (1) {
["c"]=>
object(SimpleXMLElement)#2 (1) {
["c"]=>
Comment on lines +25 to +28
Copy link
Member

Choose a reason for hiding this comment

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

Clearly those are different object but they "seem" identical?

Copy link
Member Author

Choose a reason for hiding this comment

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

This confused me too, but it is pre-existing behaviour and thus not something we can just change.
From an XML perspective it kind of makes sense: the declaration (second c) is a child of the reference (first c), etc. So that's why they show up like this.

Copy link
Member

Choose a reason for hiding this comment

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

ACK

object(SimpleXMLElement)#3 (1) {
["b"]=>
object(SimpleXMLElement)#4 (1) {
["b"]=>
object(SimpleXMLElement)#5 (1) {
["a"]=>
object(SimpleXMLElement)#6 (1) {
["a"]=>
string(9) "something"
}
}
}
}
}
}
SimpleXMLElement Object
(
[c] => SimpleXMLElement Object
(
[c] => SimpleXMLElement Object
(
[b] => SimpleXMLElement Object
(
[b] => SimpleXMLElement Object
(
[a] => SimpleXMLElement Object
(
[a] => something
)

)

)

)

)

)