]> granicus.if.org Git - php/commitdiff
fix bug #41947 (SimpleXML incorrectly registers empty strings as namespaces
authorRob Richards <rrichards@php.net>
Tue, 10 Jul 2007 12:24:51 +0000 (12:24 +0000)
committerRob Richards <rrichards@php.net>
Tue, 10 Jul 2007 12:24:51 +0000 (12:24 +0000)
add test

ext/simplexml/simplexml.c
ext/simplexml/tests/bug41947.phpt [new file with mode: 0644]

index c6e2358cc54bfde7c00c5990d1628e36631e2f5c..d480497738d03773f4773171fbccc1f560e75b4b 100644 (file)
@@ -1571,11 +1571,16 @@ SXE_METHOD(addChild)
        newnode = xmlNewChild(node, NULL, localname, (xmlChar *)value);
 
        if (nsuri != NULL) {
-               nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri);
-               if (nsptr == NULL) {
+               if (nsuri_len == 0) {
+                       newnode->ns = NULL;
                        nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix);
+               } else {
+                       nsptr = xmlSearchNsByHref(node->doc, node, (xmlChar *)nsuri);
+                       if (nsptr == NULL) {
+                               nsptr = xmlNewNs(newnode, (xmlChar *)nsuri, prefix);
+                       }
+                       newnode->ns = nsptr;
                }
-               newnode->ns = nsptr;
        }
 
        _node_as_zval(sxe, newnode, return_value, SXE_ITER_NONE, (char *)localname, prefix, 0 TSRMLS_CC);
diff --git a/ext/simplexml/tests/bug41947.phpt b/ext/simplexml/tests/bug41947.phpt
new file mode 100644 (file)
index 0000000..7af9ff8
--- /dev/null
@@ -0,0 +1,14 @@
+--TEST--
+Bug #41947 (addChild incorrectly registers empty strings as namespaces)
+--FILE--
+<?php
+$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><root xmlns:myns="http://myns" />');
+$grandchild = $xml->addChild('child', null, 'http://myns')->addChild('grandchild', 'hello', '');
+
+$gchild = $xml->xpath("//grandchild");
+if (count($gchild) > 0) {
+    echo $gchild[0];
+}
+?>
+--EXPECT--
+hello