Skip to content

Commit cb73155

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix GH-19098: libxml<2.13 segmentation fault caused by php_libxml_node_free
2 parents f1a77c0 + 3128693 commit cb73155

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

NEWS

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

19+
- LibXML:
20+
. Fixed bug GH-19098 (libxml<2.13 segmentation fault caused by
21+
php_libxml_node_free). (nielsdos)
22+
1923
- OpenSSL:
2024
. Fixed bug GH-18986 (OpenSSL backend: incorrect RAND_{load,write}_file()
2125
return value check). (nielsdos, botovq)

ext/libxml/libxml.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,26 @@ PHP_LIBXML_API void php_libxml_node_free_list(xmlNodePtr node)
341341
if (ptr->_private) {
342342
php_libxml_node_object *obj = ptr->_private;
343343
if (!obj->document || obj->document->class_type < PHP_LIBXML_CLASS_MODERN) {
344-
xmlReconciliateNs(curnode->doc, curnode);
344+
if (LIBXML_VERSION < 21300 && UNEXPECTED(curnode->doc == NULL)) {
345+
/* xmlReconciliateNs() in these versions just uses the document for xmlNewReconciledNs(),
346+
* which can create an oldNs xml namespace declaration via xmlSearchNs() -> xmlTreeEnsureXMLDecl(). */
347+
xmlDoc dummy;
348+
memset(&dummy, 0, sizeof(dummy));
349+
dummy.type = XML_DOCUMENT_NODE;
350+
curnode->doc = &dummy;
351+
xmlReconciliateNs(curnode->doc, curnode);
352+
curnode->doc = NULL;
353+
354+
/* Append oldNs to current node's nsDef, which can be at most one node. */
355+
if (dummy.oldNs) {
356+
ZEND_ASSERT(dummy.oldNs->next == NULL);
357+
xmlNsPtr old = curnode->nsDef;
358+
curnode->nsDef = dummy.oldNs;
359+
dummy.oldNs->next = old;
360+
}
361+
} else {
362+
xmlReconciliateNs(curnode->doc, curnode);
363+
}
345364
}
346365
}
347366
}

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)