Fix GH-21544: Dom\XMLDocument::C14N() drops namespace declarations on…#21561
Open
devnexen wants to merge 2 commits intophp:PHP-8.4from
Open
Fix GH-21544: Dom\XMLDocument::C14N() drops namespace declarations on…#21561devnexen wants to merge 2 commits intophp:PHP-8.4from
devnexen wants to merge 2 commits intophp:PHP-8.4from
Conversation
… on DOM-built documents. For programmatically built documents (e.g. createElementNS), namespaces live on node->ns without corresponding xmlns attributes. Create temporary synthetic nsDef entries so C14N can find them, complementing the existing xmlns attribute to nsDef conversion.
ndossche
reviewed
Mar 31, 2026
ext/dom/node.c
Outdated
Comment on lines
+2118
to
+2135
| xmlNsPtr ns = xmlMalloc(sizeof(*ns)); | ||
| if (!ns) { | ||
| return; | ||
| } | ||
|
|
||
| zval *zv = zend_hash_index_lookup(links, (zend_ulong) node); | ||
| if (Z_ISNULL_P(zv)) { | ||
| ZVAL_LONG(zv, 1); | ||
| } else { | ||
| Z_LVAL_P(zv)++; | ||
| } | ||
|
|
||
| memset(ns, 0, sizeof(*ns)); | ||
| ns->type = XML_LOCAL_NAMESPACE; | ||
| ns->href = xmlStrdup(src_ns->href); | ||
| ns->prefix = src_ns->prefix ? xmlStrdup(src_ns->prefix) : NULL; | ||
| ns->next = node->nsDef; | ||
| node->nsDef = ns; |
Member
There was a problem hiding this comment.
Perhaps it's a good idea to factor this out to a different function.
| * entries during cleanup. */ | ||
| static void dom_add_synthetic_ns_decl(HashTable *links, xmlNodePtr node, xmlNsPtr src_ns) | ||
| { | ||
| for (xmlNsPtr existing = node->nsDef; existing; existing = existing->next) { |
Member
There was a problem hiding this comment.
Do we need this loop? I believe that for Dom\XMLDocument this is normally never filled in.
Member
Author
There was a problem hiding this comment.
The loop checks existing nsDef entries to avoid duplicates. While pre-existing nsDef is indeed empty for Dom\XMLDocument, the loop is still needed:
dom_add_synthetic_ns_decl is called multiple times per node (once for node->ns, once per attr->ns), and if the element and an attribute share the same namespace
prefix, the loop prevents adding a duplicate synthetic entry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
… DOM-built documents.
For programmatically built documents (e.g. createElementNS), namespaces live on node->ns without corresponding xmlns attributes. Create temporary synthetic nsDef entries so C14N can find them, complementing the existing xmlns attribute to nsDef conversion.