Skip to content
Open
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 @@ -25,6 +25,8 @@ PHP NEWS
- DOM:
. Fixed bug GH-25268 (NULL php_libxml_ref_obj dereference in
dom_objects_store_clone_obj when cloning Dom\Notation). (David Carlier)
. Fixed use-after-free in getElementById() after a removed ID attribute is
freed. (Ilia Alshanetsky)

- MBString:
. Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with
Expand Down
56 changes: 56 additions & 0 deletions ext/dom/tests/getElementById_removed_id_attribute.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
getElementById() after a removed ID attribute is freed
--EXTENSIONS--
dom
--FILE--
<?php

$doc = new DOMDocument();
$doc->loadXML('<root><a xml:id="x"/><b xml:id="y"/></root>');
$a = $doc->documentElement->firstChild;
$attr = $a->getAttributeNode('xml:id');
$a->removeAttributeNode($attr);
unset($attr);
echo "removed id: ";
var_dump($doc->getElementById('x'));
echo "remaining id: ", $doc->getElementById('y')->nodeName, "\n";

$doc = new DOMDocument();
$doc->loadXML('<!DOCTYPE root [<!ATTLIST e id ID #IMPLIED>]><root><e id="x"/><e id="y"/></root>');
$e = $doc->documentElement->firstChild;
$attr = $e->getAttributeNode('id');
$e->removeAttributeNode($attr);
unset($attr);
echo "removed DTD id: ";
var_dump($doc->getElementById('x'));
echo "remaining DTD id: ", $doc->getElementById('y')->nodeName, "\n";

$doc = Dom\XMLDocument::createFromString('<root><a xml:id="x"/><b xml:id="y"/></root>');
$a = $doc->documentElement->firstChild;
$attr = $a->getAttributeNode('xml:id');
$a->removeAttributeNode($attr);
unset($attr);
echo "removed Dom\\XMLDocument id: ";
var_dump($doc->getElementById('x'));
echo "remaining Dom\\XMLDocument id: ", $doc->getElementById('y')->nodeName, "\n";

$doc = new DOMDocument();
$doc->loadXML('<root><a xml:id="x"/><b xml:id="y"/></root>');
$a = $doc->documentElement->firstChild;
$attr = $a->getAttributeNode('xml:id');
$a->removeAttribute('xml:id');
unset($attr);
echo "removeAttribute() id: ";
var_dump($doc->getElementById('x'));
echo "remaining removeAttribute() id: ", $doc->getElementById('y')->nodeName, "\n";

?>
--EXPECT--
removed id: NULL
remaining id: b
removed DTD id: NULL
remaining DTD id: e
removed Dom\XMLDocument id: NULL
remaining Dom\XMLDocument id: b
removeAttribute() id: NULL
remaining removeAttribute() id: b
3 changes: 3 additions & 0 deletions ext/libxml/libxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,9 @@ PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node)
break;
default:
if (node->parent == NULL || node->type == XML_NAMESPACE_DECL) {
if (node->type == XML_ATTRIBUTE_NODE && node->doc != NULL && ((xmlAttrPtr) node)->atype == XML_ATTRIBUTE_ID) {
xmlRemoveID(node->doc, (xmlAttrPtr) node);
}
php_libxml_node_free_list((xmlNodePtr) node->children);
if (node->type == XML_ELEMENT_NODE) {
php_libxml_node_free_list((xmlNodePtr) node->properties);
Expand Down
Loading