Skip to content
Merged
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PHP NEWS
. Fixed bug GH-22447 (UAF at dom_objects_free_storage when setting an
attribute node that collides by local name with a namespaced
attribute). (David Carlier)
. Fixed bug GH-23120 (Stack overflow when comparing deeply nested DOM nodes
with DOMNode::isEqualNode()). (Weilin Du)

- MBString:
. Fixed bug GH-22779 (mb_strrpos() returns the wrong position for a negative
Expand Down
23 changes: 22 additions & 1 deletion ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1730,11 +1730,27 @@ static bool php_dom_is_equal_attr(const xmlAttr *this_attr, const xmlAttr *other
&& php_dom_node_is_content_equal((const xmlNode *) this_attr, (const xmlNode *) other_attr);
}

static zend_always_inline bool php_dom_node_is_equal_node_check_stack_limit(void)
{
#ifdef ZEND_CHECK_STACK_LIMIT
return zend_call_stack_overflowed(EG(stack_limit));
#else
return false;
#endif
}

static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other, bool spec_compliant)
{
ZEND_ASSERT(this != NULL);
ZEND_ASSERT(other != NULL);

if (UNEXPECTED(php_dom_node_is_equal_node_check_stack_limit())) {
if (!EG(exception)) {
zend_throw_error(NULL, "Maximum call stack size reached.");
}
return false;
}

if (this->type != other->type) {
return false;
}
Expand Down Expand Up @@ -1795,6 +1811,7 @@ static void dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS, bool mod
zval *id, *node;
xmlNodePtr otherp, nodep;
dom_object *intern;
bool result;

id = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -1817,7 +1834,11 @@ static void dom_node_is_equal_node_common(INTERNAL_FUNCTION_PARAMETERS, bool mod
RETURN_BOOL(nodep == NULL && otherp == NULL);
}

RETURN_BOOL(php_dom_node_is_equal_node(nodep, otherp, modern));
result = php_dom_node_is_equal_node(nodep, otherp, modern);
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
RETURN_BOOL(result);
}

PHP_METHOD(DOMNode, isEqualNode)
Expand Down
42 changes: 42 additions & 0 deletions ext/dom/tests/gh23120.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
GH-23120 (Stack overflow when comparing deeply nested DOM nodes)
--EXTENSIONS--
dom
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
if (getenv('SKIP_ASAN')) {
die('skip ASAN needs different stack limit setting due to more stack space usage');
}
?>
--INI--
zend.max_allowed_stack_size=512K
--FILE--
<?php
function create_deep_document(): DOMDocument {
$doc = new DOMDocument();
$node = $doc->createElement('leaf', 'x');

for ($i = 0; $i < 10000; $i++) {
$parent = $doc->createElement('a');
$parent->appendChild($node);
$node = $parent;
}

$doc->appendChild($node);
return $doc;
}

$doc1 = create_deep_document();
$doc2 = create_deep_document();

try {
var_dump($doc1->documentElement->isEqualNode($doc2->documentElement));
} catch (\Error $e) {
echo $e::class, ": ", $e->getMessage(), "\n";
}
?>
--EXPECT--
Error: Maximum call stack size reached.
Loading