]> granicus.if.org Git - php/commitdiff
fixed bug #45251 (double free or corruption with setAttributeNode())
authorRob Richards <rrichards@php.net>
Sat, 14 Jun 2008 11:21:47 +0000 (11:21 +0000)
committerRob Richards <rrichards@php.net>
Sat, 14 Jun 2008 11:21:47 +0000 (11:21 +0000)
add test

ext/dom/element.c
ext/dom/tests/bug45251.phpt [new file with mode: 0644]

index 3e0ae3c69a2383f57366e7a56fb37b0f902ad267..019ff19f8e72da0ae88e51a0ec437ebc5c400bfb 100644 (file)
@@ -592,6 +592,10 @@ PHP_FUNCTION(dom_element_set_attribute_node)
                xmlUnlinkNode((xmlNodePtr) existattrp);
        }
 
+       if (attrp->parent != NULL) {
+               xmlUnlinkNode((xmlNodePtr) attrp);
+       }
+
        if (attrp->doc == NULL && nodep->doc != NULL) {
                attrobj->document = intern->document;
                php_libxml_increment_doc_ref((php_libxml_node_object *)attrobj, NULL TSRMLS_CC);
@@ -1012,6 +1016,10 @@ PHP_FUNCTION(dom_element_set_attribute_node_ns)
                xmlUnlinkNode((xmlNodePtr) existattrp);
        }
 
+       if (attrp->parent != NULL) {
+               xmlUnlinkNode((xmlNodePtr) attrp);
+       }
+
        if (attrp->doc == NULL && nodep->doc != NULL) {
                attrobj->document = intern->document;
                php_libxml_increment_doc_ref((php_libxml_node_object *)attrobj, NULL TSRMLS_CC);
diff --git a/ext/dom/tests/bug45251.phpt b/ext/dom/tests/bug45251.phpt
new file mode 100644 (file)
index 0000000..652e3b2
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+Bug #45251 (double free or corruption with setAttributeNode())
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--FILE--
+<?php
+$doc = new DOMDocument;
+$doc->loadXml(<<<EOF
+<?xml version="1.0" encoding="utf-8" ?>
+<aaa>
+  <bbb foo="bar"/>
+</aaa>
+EOF
+);
+
+$xpath = new DOMXPath($doc);
+
+$bbb = $xpath->query('bbb', $doc->documentElement)->item(0);
+
+$ccc = $doc->createElement('ccc');
+foreach ($bbb->attributes as $attr)
+{
+  $ccc->setAttributeNode($attr);
+}
+
+echo $attr->parentNode->localName;
+
+?>
+--EXPECT--
+ccc