diff --git a/ext/dom/node.c b/ext/dom/node.c index d053fbceb06..fbf1f327e4d 100644 --- a/ext/dom/node.c +++ b/ext/dom/node.c @@ -674,8 +674,14 @@ zend_result dom_node_prefix_write(dom_object *obj, zval *newval) prefix_str = Z_STR_P(newval); prefix = ZSTR_VAL(prefix_str); + if (*prefix == '\0') { + /* The empty string namespace prefix does not exist. + * We should fall back to the default namespace in this case. */ + prefix = NULL; + } if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) { strURI = (char *) nodep->ns->href; + /* Validate namespace naming constraints */ if (strURI == NULL || (zend_string_equals_literal(prefix_str, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) || (nodep->type == XML_ATTRIBUTE_NODE && zend_string_equals_literal(prefix_str, "xmlns") && diff --git a/ext/dom/tests/DOMElement_prefix_empty.phpt b/ext/dom/tests/DOMElement_prefix_empty.phpt new file mode 100644 index 00000000000..8cd3515889e --- /dev/null +++ b/ext/dom/tests/DOMElement_prefix_empty.phpt @@ -0,0 +1,51 @@ +--TEST-- +DOMElement->prefix with empty string creates bogus prefix +--EXTENSIONS-- +dom +--FILE-- +loadXML(""); + +$container = $dom->documentElement; + +echo "--- Changing the prefix to an empty string ---\n"; + +$container->prefix = ""; +echo $dom->saveXML(); + +echo "--- Changing the prefix to an empty C-style string ---\n"; + +$container->prefix = "\0foobar"; +echo $dom->saveXML(); + +echo "--- Changing the prefix to \"hello\" ---\n"; + +$container->prefix = "hello"; +echo $dom->saveXML(); + +echo "--- Changing the prefix to that of a conflicting namespace (\"conflict\") ---\n"; + +try { + $container->prefix = "conflict"; +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveXML(); + +?> +--EXPECT-- +--- Changing the prefix to an empty string --- + + +--- Changing the prefix to an empty C-style string --- + + +--- Changing the prefix to "hello" --- + + +--- Changing the prefix to that of a conflicting namespace ("conflict") --- +Namespace Error + +