Skip to content

Commit e013b4a

Browse files
committed
Make cloning DOM node lists, maps, and collections fail
This never worked and creates a broken object, and on master can cause a crash with foreach. It makes no sense to fix a behaviour that never worked, block it instead. Closes GH-19089.
1 parent f6380e4 commit e013b4a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.5.0alpha2
44

5+
- DOM:
6+
. Make cloning DOM node lists, maps, and collections fail. (nielsdos)
7+
58
- PDO_ODBC
69
. Fetch larger block sizes and better handle SQL_NO_TOTAL when calling
710
SQLGetData. (Calvin Buckley, Saki Takamachi)

UPGRADING

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ PHP 8.5 UPGRADE NOTES
4545
change, but should more closely match user expectations, demonstrated by
4646
GH-15753 and GH-16198.
4747

48+
- DOM:
49+
. Cloning a DOMNamedNodeMap, DOMNodeList, Dom\NamedNodeMap, Dom\NodeList,
50+
Dom\HTMLCollection, and Dom\DtdNamedNodeMap now fails.
51+
This never actually resulted in a working object,
52+
so the impact should actually be zero.
53+
4854
- FileInfo:
4955
. finfo_file() and finfo::file() now throws a ValueError instead of a
5056
TypeError when $filename contains nul bytes.

ext/dom/php_dom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ PHP_MINIT_FUNCTION(dom)
806806
dom_nnodemap_object_handlers.free_obj = dom_nnodemap_objects_free_storage;
807807
dom_nnodemap_object_handlers.read_dimension = dom_nodemap_read_dimension;
808808
dom_nnodemap_object_handlers.has_dimension = dom_nodemap_has_dimension;
809+
dom_nnodemap_object_handlers.clone_obj = NULL;
809810

810811
memcpy(&dom_nodelist_object_handlers, &dom_nnodemap_object_handlers, sizeof(zend_object_handlers));
811812
dom_nodelist_object_handlers.read_dimension = dom_nodelist_read_dimension;
@@ -823,7 +824,6 @@ PHP_MINIT_FUNCTION(dom)
823824
dom_html_collection_object_handlers.read_dimension = dom_html_collection_read_dimension;
824825
dom_html_collection_object_handlers.has_dimension = dom_html_collection_has_dimension;
825826
dom_html_collection_object_handlers.get_gc = dom_html_collection_get_gc;
826-
dom_html_collection_object_handlers.clone_obj = NULL;
827827

828828
memcpy(&dom_object_namespace_node_handlers, &dom_object_handlers, sizeof(zend_object_handlers));
829829
dom_object_namespace_node_handlers.offset = XtOffsetOf(dom_object_namespace_node, dom.std);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Cloning node lists, maps, and collections should fail
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument;
9+
$dom->loadXML('<root a="1"><a/></root>');
10+
try {
11+
clone $dom->documentElement->attributes;
12+
} catch (Error $e) {
13+
echo $e->getMessage(), "\n";
14+
}
15+
try {
16+
clone $dom->documentElement->childNodes;
17+
} catch (Error $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
21+
$dom = Dom\XMLDocument::createFromString('<!DOCTYPE root [<!ENTITY foo "">]><root a="1"><a/></root>');
22+
try {
23+
clone $dom->documentElement->attributes;
24+
} catch (Error $e) {
25+
echo $e->getMessage(), "\n";
26+
}
27+
try {
28+
clone $dom->documentElement->childNodes;
29+
} catch (Error $e) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
try {
33+
clone $dom->documentElement->children;
34+
} catch (Error $e) {
35+
echo $e->getMessage(), "\n";
36+
}
37+
try {
38+
clone $dom->doctype->entities;
39+
} catch (Error $e) {
40+
echo $e->getMessage(), "\n";
41+
}
42+
43+
?>
44+
--EXPECT--
45+
Trying to clone an uncloneable object of class DOMNamedNodeMap
46+
Trying to clone an uncloneable object of class DOMNodeList
47+
Trying to clone an uncloneable object of class Dom\NamedNodeMap
48+
Trying to clone an uncloneable object of class Dom\NodeList
49+
Trying to clone an uncloneable object of class Dom\HTMLCollection
50+
Trying to clone an uncloneable object of class Dom\DtdNamedNodeMap

0 commit comments

Comments
 (0)