]> granicus.if.org Git - php/commitdiff
Fix #76712: Assignment of empty string creates extraneous text node
authorChristoph M. Becker <cmbecker69@gmx.de>
Tue, 7 Aug 2018 09:37:58 +0000 (11:37 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Sat, 25 Aug 2018 12:24:09 +0000 (14:24 +0200)
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
ext/simplexml/simplexml.c
ext/simplexml/tests/bug76712.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 4aa88bffd500a2c0cec0710a74ada95b60e4f675..bf8fe5b9af1f1dbb6a26d27790ee8d34a701cf74 100644 (file)
--- 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
index 92e6de107ac181d22d26828e83c333d045fdbd26..ab394b5c83b76659991e3963df5568c3f442b716 100644 (file)
@@ -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 (file)
index 0000000..efd34b5
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+BUg #76712 (Assignment of empty string creates extraneous text node)
+--SKIPIF--
+<?php
+if (!extension_loaded('simplexml')) die('skip simplexml not available');
+?>
+--FILE--
+<?php
+$sxe = new SimpleXMLElement('<foo></foo>');
+$sxe->addChild('bar', '');
+echo $sxe->asXML();
+
+$sxe = new SimpleXMLElement('<foo></foo>');
+$sxe->addChild('bar');
+$sxe->bar = '';
+echo $sxe->asXML();
+?>
+===DONE===
+--EXPECT--
+<?xml version="1.0"?>
+<foo><bar/></foo>
+<?xml version="1.0"?>
+<foo><bar/></foo>
+===DONE===