From 41e02dad89fa6d28f3944d3f81453cff595ce6b4 Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Mon, 12 Feb 2007 21:13:01 +0000 Subject: [PATCH] MFB: #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 ef8f675302..9d4385a3ae 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -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 index 0000000000..949dc87575 --- /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((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 -- 2.40.0