From 2c31b1e194adc746597eb9ab2310e87e01bf86d4 Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Mon, 12 Feb 2007 21:06:29 +0000 Subject: [PATCH] fix #40451 (addAttribute() may crash when used with non-existent child node) --- ext/simplexml/simplexml.c | 2 +- ext/simplexml/tests/bug40451.phpt | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 ext/simplexml/tests/bug40451.phpt diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index 6695f47301..603d1c5f8a 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -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 index 0000000000..1a499a731d --- /dev/null +++ b/ext/simplexml/tests/bug40451.phpt @@ -0,0 +1,22 @@ +--TEST-- +Bug #40451 (addAttribute() may crash when used with non-existent child node) +--FILE-- + + + host.server.com + +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 -- 2.50.1