Skip to content

Commit 76ad89c

Browse files
committed
Fix GH-15192: Segmentation fault in dom extension (html5_serializer)
When cloning a document, doc will not be equal to the actual new document clone->doc. clone->doc will always point to the correct document so use that instead when comparing document nodes. Closes GH-15198.
1 parent 85fa983 commit 76ad89c

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.4.0beta1
44

5+
- DOM:
6+
. Fixed bug GH-15192 (Segmentation fault in dom extension
7+
(html5_serializer)). (nielsdos)
8+
59
- PHPDBG:
610
. array out of bounds, stack overflow handled for segfault handler on windows.
711
(David Carlier)
812

9-
1013
01 Aug 2024, PHP 8.4.0alpha4
1114

1215
- GMP:

ext/dom/php_dom.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2579,7 +2579,10 @@ xmlNodePtr dom_clone_node(php_dom_libxml_ns_mapper *ns_mapper, xmlNodePtr node,
25792579

25802580
if (ns_mapper != NULL) {
25812581
xmlNodePtr clone = dom_clone_helper(ns_mapper, node, doc, recursive);
2582-
if (EXPECTED(clone != NULL) && doc != node->doc) {
2582+
/* Defensively set doc to NULL because we should not be using it after this point.
2583+
* When cloning a document the new document will be clone->doc, not doc. */
2584+
doc = NULL;
2585+
if (EXPECTED(clone != NULL) && clone->doc != node->doc) {
25832586
/* We only need to reconcile the namespace when the document changes because the namespaces have to be
25842587
* put into their respective namespace mapper. */
25852588
if (clone->type == XML_DOCUMENT_NODE || clone->type == XML_HTML_DOCUMENT_NODE || clone->type == XML_DOCUMENT_FRAG_NODE) {

ext/dom/tests/gh15192.phpt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
GH-15192 (Segmentation fault in dom extension (html5_serializer))
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$dom = @Dom\HTMLDocument::createFromString("<p>foo</p>");
8+
$dom2 = clone $dom;
9+
$element = $dom2->firstChild;
10+
$dom = new DomDocument();
11+
var_dump($element);
12+
?>
13+
--EXPECTF--
14+
object(Dom\HTMLElement)#3 (29) {
15+
["namespaceURI"]=>
16+
string(28) "http://www.w3.org/1999/xhtml"
17+
["prefix"]=>
18+
NULL
19+
["localName"]=>
20+
string(4) "html"
21+
["tagName"]=>
22+
string(4) "HTML"
23+
["id"]=>
24+
string(0) ""
25+
["className"]=>
26+
string(0) ""
27+
["classList"]=>
28+
string(22) "(object value omitted)"
29+
["attributes"]=>
30+
string(22) "(object value omitted)"
31+
["firstElementChild"]=>
32+
string(22) "(object value omitted)"
33+
["lastElementChild"]=>
34+
string(22) "(object value omitted)"
35+
["childElementCount"]=>
36+
int(2)
37+
["previousElementSibling"]=>
38+
NULL
39+
["nextElementSibling"]=>
40+
NULL
41+
["innerHTML"]=>
42+
string(36) "<head></head><body><p>foo</p></body>"
43+
["substitutedNodeValue"]=>
44+
string(3) "foo"
45+
["nodeType"]=>
46+
int(1)
47+
["nodeName"]=>
48+
string(4) "HTML"
49+
["baseURI"]=>
50+
string(11) "about:blank"
51+
["isConnected"]=>
52+
bool(true)
53+
["ownerDocument"]=>
54+
string(22) "(object value omitted)"
55+
["parentNode"]=>
56+
string(22) "(object value omitted)"
57+
["parentElement"]=>
58+
NULL
59+
["childNodes"]=>
60+
string(22) "(object value omitted)"
61+
["firstChild"]=>
62+
string(22) "(object value omitted)"
63+
["lastChild"]=>
64+
string(22) "(object value omitted)"
65+
["previousSibling"]=>
66+
NULL
67+
["nextSibling"]=>
68+
NULL
69+
["nodeValue"]=>
70+
NULL
71+
["textContent"]=>
72+
string(3) "foo"
73+
}

0 commit comments

Comments
 (0)