Skip to content

Commit a0c941a

Browse files
author
Rob Richards
committed
fix bug #38424 (Different attribute assignment if new or existing)
add test
1 parent 3048cd7 commit a0c941a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

ext/simplexml/simplexml.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ static zval * sxe_dimension_read(zval *object, zval *offset, int type TSRMLS_DC)
366366
static void change_node_zval(xmlNodePtr node, zval *value TSRMLS_DC)
367367
{
368368
zval value_copy;
369+
xmlChar *buffer;
370+
int buffer_len;
369371

370372
if (!value)
371373
{
@@ -385,7 +387,20 @@ static void change_node_zval(xmlNodePtr node, zval *value TSRMLS_DC)
385387
convert_to_string(value);
386388
/* break missing intentionally */
387389
case IS_STRING:
388-
xmlNodeSetContentLen(node, (xmlChar *)Z_STRVAL_P(value), Z_STRLEN_P(value));
390+
if (node->type == XML_ATTRIBUTE_NODE) {
391+
buffer = xmlEncodeEntitiesReentrant(node->doc, (xmlChar *)Z_STRVAL_P(value));
392+
buffer_len = xmlStrlen(buffer);
393+
} else {
394+
buffer = (xmlChar *)Z_STRVAL_P(value);
395+
buffer_len = Z_STRLEN_P(value);
396+
}
397+
/* check for NULL buffer in case of memory error in xmlEncodeEntitiesReentrant */
398+
if (buffer) {
399+
xmlNodeSetContentLen(node, buffer, buffer_len);
400+
if (node->type == XML_ATTRIBUTE_NODE) {
401+
xmlFree(buffer);
402+
}
403+
}
389404
if (value == &value_copy) {
390405
zval_dtor(value);
391406
}

ext/simplexml/tests/bug38424.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #38424 (Different attribute assignment if new or exists)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
$xml = simplexml_load_string('<xml></xml>');
9+
10+
$str = "abc & def" ;
11+
12+
$xml["a1"] = "" ;
13+
$xml["a1"] = htmlspecialchars($str,ENT_NOQUOTES) ;
14+
15+
$xml["a2"] = htmlspecialchars($str,ENT_NOQUOTES) ;
16+
17+
$xml["a3"] = "" ;
18+
$xml["a3"] = $str ;
19+
20+
$xml["a4"] = $str ;
21+
22+
echo $xml->asXML();
23+
?>
24+
--EXPECT--
25+
<?xml version="1.0"?>
26+
<xml a1="abc &amp;amp; def" a2="abc &amp;amp; def" a3="abc &amp; def" a4="abc &amp; def"/>

0 commit comments

Comments
 (0)