Skip to content

Commit 532a260

Browse files
committed
Fix GH-14698: segfault on dom node after dereference.
close GH-14701
1 parent 03dab7e commit 532a260

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ PHP NEWS
5656
. Fixed bug #79701 (getElementById does not correctly work with duplicate
5757
definitions). (nielsdos)
5858
. Implemented "New ext-dom features in PHP 8.4" RFC. (nielsdos)
59+
. Fixed GH-14698 (segfault on DOM node dereference). (David Carlier)
5960

6061
- Fileinfo:
6162
. Update to libmagic 5.45. (nielsdos)

ext/dom/tests/gh14698.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-14698 crash on DOM node dereference
3+
--EXTENSIONS--
4+
dom
5+
--CREDITS--
6+
YuanchengJiang
7+
--FILE--
8+
<?php
9+
$dom = new DOMDocument;
10+
$dom->loadHTML('<span title="y">x</span><span title="z">x</span>');
11+
$html = simplexml_import_dom($dom);
12+
foreach ($html->body->span as $obj) {
13+
}
14+
$script1_dataflow = $html;
15+
$array = ['foo'];
16+
foreach ($array as $key => &$value) {
17+
unset($script1_dataflow[$key]);
18+
}
19+
echo "DONE";
20+
?>
21+
--EXPECTF--
22+
DONE

ext/libxml/libxml.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,13 @@ PHP_LIBXML_API void php_libxml_node_free_list(xmlNodePtr node)
329329
/* This ensures that namespace references in this subtree are defined within this subtree,
330330
* otherwise a use-after-free would be possible when the original namespace holder gets freed. */
331331
php_libxml_node_ptr *ptr = curnode->_private;
332-
php_libxml_node_object *obj = ptr->_private;
333-
if (!obj->document || obj->document->class_type < PHP_LIBXML_CLASS_MODERN) {
334-
xmlReconciliateNs(curnode->doc, curnode);
332+
333+
/* Checking in case it runs out of reference */
334+
if (ptr->_private) {
335+
php_libxml_node_object *obj = ptr->_private;
336+
if (!obj->document || obj->document->class_type < PHP_LIBXML_CLASS_MODERN) {
337+
xmlReconciliateNs(curnode->doc, curnode);
338+
}
335339
}
336340
}
337341
/* Skip freeing */

0 commit comments

Comments
 (0)