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;
}
--- /dev/null
+--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