From 692e5d5c88a939a7c3ce3de61c5fd39effe7c7ae Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 7 Aug 2018 11:37:58 +0200 Subject: [PATCH] Fix #76712: Assignment of empty string creates extraneous text node We work around this peculiarity of libxml by using xmlNodeSetContent(), which does not exhibit this behavior. This also saves us from manually calculating the string length. --- NEWS | 4 ++++ ext/simplexml/simplexml.c | 4 +--- ext/simplexml/tests/bug76712.phpt | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ext/simplexml/tests/bug76712.phpt diff --git a/NEWS b/NEWS index 4aa88bffd5..bf8fe5b9af 100644 --- a/NEWS +++ b/NEWS @@ -24,6 +24,10 @@ PHP NEWS . Fixed bug #76747 (Opcache treats path containing "test.pharma.tld" as a phar file). (Laruence) +- SimpleXML: + . Fixed bug #76712 (Assignment of empty string creates extraneous text node). + (cmb) + - SPL: . Fixed bug #68825 (Exception in DirectoryIterator::getLinkTarget()). (cmb) . Fixed bug #68175 (RegexIterator pregFlags are NULL instead of 0). (Tim diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index 92e6de107a..ab394b5c83 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -384,7 +384,6 @@ static zval *sxe_dimension_read(zval *object, zval *offset, int type, zval *rv) static void change_node_zval(xmlNodePtr node, zval *value) { xmlChar *buffer; - int buffer_len; if (!value) { @@ -401,10 +400,9 @@ static void change_node_zval(xmlNodePtr node, zval *value) /* break missing intentionally */ case IS_STRING: buffer = xmlEncodeEntitiesReentrant(node->doc, (xmlChar *)Z_STRVAL_P(value)); - buffer_len = xmlStrlen(buffer); /* check for NULL buffer in case of memory error in xmlEncodeEntitiesReentrant */ if (buffer) { - xmlNodeSetContentLen(node, buffer, buffer_len); + xmlNodeSetContent(node, buffer); xmlFree(buffer); } break; diff --git a/ext/simplexml/tests/bug76712.phpt b/ext/simplexml/tests/bug76712.phpt new file mode 100644 index 0000000000..efd34b56e2 --- /dev/null +++ b/ext/simplexml/tests/bug76712.phpt @@ -0,0 +1,24 @@ +--TEST-- +BUg #76712 (Assignment of empty string creates extraneous text node) +--SKIPIF-- + +--FILE-- +'); +$sxe->addChild('bar', ''); +echo $sxe->asXML(); + +$sxe = new SimpleXMLElement(''); +$sxe->addChild('bar'); +$sxe->bar = ''; +echo $sxe->asXML(); +?> +===DONE=== +--EXPECT-- + + + + +===DONE=== -- 2.50.1