Skip to content

Commit 9bd9e9a

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Fix #79451: DOMDocument->replaceChild on doctype causes double free
2 parents 9360cd6 + 6027d44 commit 9bd9e9a

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ PHP NEWS
66
. Fixed bug GH-9323 (Crash in ZEND_RETURN/GC/zend_call_function)
77
(Tim Starling)
88

9+
- DOM:
10+
. Fixed bug #79451 (DOMDocument->replaceChild on doctype causes double free).
11+
(Nathan Freeman)
12+
913
- Streams:
1014
. Fixed bug GH-9316 ($http_response_header is wrong for long status line).
1115
(cmb, timwolla)

ext/dom/node.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ PHP_METHOD(DOMNode, replaceChild)
10031003
xmlNodePtr newchild, oldchild, nodep;
10041004
dom_object *intern, *newchildobj, *oldchildobj;
10051005
int stricterror;
1006+
bool replacedoctype = false;
10061007

10071008
int ret;
10081009

@@ -1059,13 +1060,20 @@ PHP_METHOD(DOMNode, replaceChild)
10591060
dom_reconcile_ns(nodep->doc, newchild);
10601061
}
10611062
} else if (oldchild != newchild) {
1063+
xmlDtdPtr intSubset = xmlGetIntSubset(nodep->doc);
1064+
replacedoctype = (intSubset == (xmlDtd *) oldchild);
1065+
10621066
if (newchild->doc == NULL && nodep->doc != NULL) {
10631067
xmlSetTreeDoc(newchild, nodep->doc);
10641068
newchildobj->document = intern->document;
10651069
php_libxml_increment_doc_ref((php_libxml_node_object *)newchildobj, NULL);
10661070
}
10671071
xmlReplaceNode(oldchild, newchild);
10681072
dom_reconcile_ns(nodep->doc, newchild);
1073+
1074+
if (replacedoctype) {
1075+
nodep->doc->intSubset = (xmlDtd *) newchild;
1076+
}
10691077
}
10701078
DOM_RET_OBJ(oldchild, &ret, intern);
10711079
}

ext/dom/tests/bug79451.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--TEST--
22
Bug #79451 (Using DOMDocument->replaceChild on doctype causes double free)
3-
--SKIPIF--
4-
<?php require_once('skipif.inc'); ?>
3+
--EXTENSIONS--
4+
dom
55
--FILE--
66
<?php
77
$dom = new \DOMDocument();

0 commit comments

Comments
 (0)