]> granicus.if.org Git - php/commitdiff
MFB: #40451 (addAttribute() may crash when used with non-existent child node)
authorAntony Dovgal <tony2001@php.net>
Mon, 12 Feb 2007 21:13:01 +0000 (21:13 +0000)
committerAntony Dovgal <tony2001@php.net>
Mon, 12 Feb 2007 21:13:01 +0000 (21:13 +0000)
ext/simplexml/simplexml.c
ext/simplexml/tests/bug40451.phpt [new file with mode: 0644]

index ef8f6753028cd3b04f57a3b52f33ea4a0ed0ea1d..9d4385a3ae8daa2f6c427ae509f7509c5454b299 100644 (file)
@@ -1586,7 +1586,7 @@ SXE_METHOD(addAttribute)
 
        node = php_sxe_get_first_node(sxe, node TSRMLS_CC);
 
-       if (node->type != XML_ELEMENT_NODE) {
+       if (node && node->type != XML_ELEMENT_NODE) {
                node = node->parent;
        }
 
diff --git a/ext/simplexml/tests/bug40451.phpt b/ext/simplexml/tests/bug40451.phpt
new file mode 100644 (file)
index 0000000..949dc87
--- /dev/null
@@ -0,0 +1,22 @@
+--TEST--
+Bug #40451 (addAttribute() may crash when used with non-existent child node)
+--FILE--
+<?php
+
+$string = <<<XML
+<?xml version="1.0"?>
+       <Host enable="true">
+        <Name>host.server.com</Name>
+        </Host>
+XML;
+
+$xml = simplexml_load_string((binary)$string);
+
+$add = $xml->addChild('Host');
+$add->Host->addAttribute('enable', 'true');
+
+echo "Done\n";
+?>
+--EXPECTF--    
+Warning: SimpleXMLElement::addAttribute(): Unable to locate parent Element in %s on line %d
+Done