Skip to content

Commit 692e5d5

Browse files
committed
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.
1 parent fcf4088 commit 692e5d5

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ PHP NEWS
2424
. Fixed bug #76747 (Opcache treats path containing "test.pharma.tld" as a phar
2525
file). (Laruence)
2626

27+
- SimpleXML:
28+
. Fixed bug #76712 (Assignment of empty string creates extraneous text node).
29+
(cmb)
30+
2731
- SPL:
2832
. Fixed bug #68825 (Exception in DirectoryIterator::getLinkTarget()). (cmb)
2933
. Fixed bug #68175 (RegexIterator pregFlags are NULL instead of 0). (Tim

ext/simplexml/simplexml.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ static zval *sxe_dimension_read(zval *object, zval *offset, int type, zval *rv)
384384
static void change_node_zval(xmlNodePtr node, zval *value)
385385
{
386386
xmlChar *buffer;
387-
int buffer_len;
388387

389388
if (!value)
390389
{
@@ -401,10 +400,9 @@ static void change_node_zval(xmlNodePtr node, zval *value)
401400
/* break missing intentionally */
402401
case IS_STRING:
403402
buffer = xmlEncodeEntitiesReentrant(node->doc, (xmlChar *)Z_STRVAL_P(value));
404-
buffer_len = xmlStrlen(buffer);
405403
/* check for NULL buffer in case of memory error in xmlEncodeEntitiesReentrant */
406404
if (buffer) {
407-
xmlNodeSetContentLen(node, buffer, buffer_len);
405+
xmlNodeSetContent(node, buffer);
408406
xmlFree(buffer);
409407
}
410408
break;

ext/simplexml/tests/bug76712.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
BUg #76712 (Assignment of empty string creates extraneous text node)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('simplexml')) die('skip simplexml not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$sxe = new SimpleXMLElement('<foo></foo>');
10+
$sxe->addChild('bar', '');
11+
echo $sxe->asXML();
12+
13+
$sxe = new SimpleXMLElement('<foo></foo>');
14+
$sxe->addChild('bar');
15+
$sxe->bar = '';
16+
echo $sxe->asXML();
17+
?>
18+
===DONE===
19+
--EXPECT--
20+
<?xml version="1.0"?>
21+
<foo><bar/></foo>
22+
<?xml version="1.0"?>
23+
<foo><bar/></foo>
24+
===DONE===

0 commit comments

Comments
 (0)