Skip to content

Commit a4aa4f9

Browse files
committed
Fix bug #66502: DOM document dangling reference
When we decrement the refcount of a node's document, we state that we won't need it anymore. Therefore we can *always* set the pointer to the document to NULL, what avoids invalid memory accesses for some edge cases as demonstrated with the PHPT. Original patch provided by Sean Heelan.
1 parent 1c84b55 commit a4aa4f9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ PHP NEWS
1616
. Fixed bug #66836 (DateTime::createFromFormat 'U' with pre 1970 dates fails
1717
parsing). (derick)
1818

19+
- DOM:
20+
. Fixed bug #66502 (DOM document dangling reference). (Sean Heelan, cmb)
21+
1922
- Filter:
2023
. Fixed bug #71745 (FILTER_FLAG_NO_RES_RANGE does not cover whole 127.0.0.0/8
2124
range). (bugs dot php dot net at majkl578 dot cz)

ext/dom/tests/bug66502.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #66502 (DOM document dangling reference)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('dom')) die('skip requires ext/dom');
6+
?>
7+
--FILE--
8+
<?php
9+
$dom = new DOMDocument('1.0', 'UTF-8');
10+
$element = $dom->appendChild(new DOMElement('root'));
11+
$comment = new DOMComment("Comment 0");
12+
$comment = $element->appendChild($comment);
13+
14+
$comment->__construct("Comment 1");
15+
$comment->__construct("Comment 2");
16+
$comment->__construct("Comment 3");
17+
echo 'DONE', PHP_EOL;
18+
?>
19+
--EXPECT--
20+
DONE

ext/libxml/libxml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,8 +1272,8 @@ PHP_LIBXML_API int php_libxml_decrement_doc_ref(php_libxml_node_object *object T
12721272
efree(object->document->doc_props);
12731273
}
12741274
efree(object->document);
1275-
object->document = NULL;
12761275
}
1276+
object->document = NULL;
12771277
}
12781278

12791279
return ret_refcount;

0 commit comments

Comments
 (0)