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

index 6695f473017a4fbad5bd91f4483fb9948890a774..603d1c5f8af433c4cf23c4e435ecaaa5fa82d008 100644 (file)
@@ -1526,7 +1526,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..1a499a7
--- /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($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