Skip to content

Commit 3128693

Browse files
committed
Fix GH-19098: libxml<2.13 segmentation fault caused by php_libxml_node_free
This implements a workaround for reconciliation not being performed for document-less nodes in libxml<2.13. Closes GH-19186.
1 parent be09985 commit 3128693

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ PHP NEWS
1313
. Fixed bug GH-18529 (additional inheriting of TLS int options).
1414
(Jakub Zelenka)
1515

16+
- LibXML:
17+
. Fixed bug GH-19098 (libxml<2.13 segmentation fault caused by
18+
php_libxml_node_free). (nielsdos)
19+
1620
- OpenSSL:
1721
. Fixed bug GH-18986 (OpenSSL backend: incorrect RAND_{load,write}_file()
1822
return value check). (nielsdos, botovq)

ext/libxml/libxml.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,26 @@ PHP_LIBXML_API void php_libxml_node_free_list(xmlNodePtr node)
331331
if (curnode->type == XML_ELEMENT_NODE) {
332332
/* This ensures that namespace references in this subtree are defined within this subtree,
333333
* otherwise a use-after-free would be possible when the original namespace holder gets freed. */
334-
#if 0
335-
xmlDOMWrapCtxt dummy_ctxt = {0};
336-
xmlDOMWrapReconcileNamespaces(&dummy_ctxt, curnode, /* options */ 0);
337-
#else
338-
/* See php_dom.c */
339-
xmlReconciliateNs(curnode->doc, curnode);
340-
#endif
334+
if (LIBXML_VERSION < 21300 && UNEXPECTED(curnode->doc == NULL)) {
335+
/* xmlReconciliateNs() in these versions just uses the document for xmlNewReconciledNs(),
336+
* which can create an oldNs xml namespace declaration via xmlSearchNs() -> xmlTreeEnsureXMLDecl(). */
337+
xmlDoc dummy;
338+
memset(&dummy, 0, sizeof(dummy));
339+
dummy.type = XML_DOCUMENT_NODE;
340+
curnode->doc = &dummy;
341+
xmlReconciliateNs(curnode->doc, curnode);
342+
curnode->doc = NULL;
343+
344+
/* Append oldNs to current node's nsDef, which can be at most one node. */
345+
if (dummy.oldNs) {
346+
ZEND_ASSERT(dummy.oldNs->next == NULL);
347+
xmlNsPtr old = curnode->nsDef;
348+
curnode->nsDef = dummy.oldNs;
349+
dummy.oldNs->next = old;
350+
}
351+
} else {
352+
xmlReconciliateNs(curnode->doc, curnode);
353+
}
341354
}
342355
/* Skip freeing */
343356
curnode = next;

ext/xmlreader/tests/gh19098.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
GH-19098 (libxml<2.13 segmentation fault caused by php_libxml_node_free)
3+
--EXTENSIONS--
4+
xmlreader
5+
dom
6+
--FILE--
7+
<?php
8+
9+
$xml_reader = \XMLReader::XML('
10+
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
11+
<results>
12+
<result><binding xml:id="foo" xmlns:custom="urn:custom" custom:foo="bar" name="s"><uri/></binding></result>
13+
</results>
14+
</sparql>');
15+
16+
$success = $xml_reader->next("sparql");
17+
18+
$success = $xml_reader->read();
19+
$success = $xml_reader->next("results");
20+
21+
while ($xml_reader->read()) {
22+
if ($xml_reader->next("result")) {
23+
$result_as_dom_node = $xml_reader->expand();
24+
$child = $result_as_dom_node->firstChild;
25+
unset($result_as_dom_node);
26+
var_dump($child->namespaceURI);
27+
foreach ($child->attributes as $attr) {
28+
var_dump($attr->namespaceURI);
29+
}
30+
$doc = new DOMDocument;
31+
$doc->adoptNode($child);
32+
echo $doc->saveXML($child), "\n";
33+
unset($child);
34+
break;
35+
}
36+
}
37+
38+
?>
39+
--EXPECT--
40+
string(38) "http://www.w3.org/2005/sparql-results#"
41+
string(36) "http://www.w3.org/XML/1998/namespace"
42+
string(10) "urn:custom"
43+
NULL
44+
<default:binding xmlns:custom="urn:custom" xmlns:default="http://www.w3.org/2005/sparql-results#" xml:id="foo" custom:foo="bar" name="s"><default:uri/></default:binding>

0 commit comments

Comments
 (0)