Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ PHP 8.7 UPGRADE NOTES
curl_setopt() return false, instead of being silently interpreted as a
combination of redirect types.

- DOM:
. Dom\import_simplexml() now throws a TypeError when the node already has a
legacy DOMNode object attached and its document is not yet locked to a
model. Previously it returned that legacy object, which violated the
Attr|Element return type.

- PDO:
. PDOStatement::bindColumn() now throws a ValueError when the column name is
not present in the result set. It previously reported the condition
Expand Down
6 changes: 6 additions & 0 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,12 @@ static void dom_import_simplexml_common(INTERNAL_FUNCTION_PARAMETERS, php_libxml
RETURN_THROWS();
}

if (old_class_type == PHP_LIBXML_CLASS_UNSET && new_class == PHP_LIBXML_CLASS_MODERN
&& php_dom_object_get_data(nodep) != NULL) {
zend_argument_type_error(1, "must not be already imported as a DOMNode");
RETURN_THROWS();
}

/* Lock the node class type to prevent creating multiple representations of the same node. */
nodeobj->document->class_type = new_class;

Expand Down
42 changes: 42 additions & 0 deletions ext/dom/tests/modern/xml/import_simplexml_legacy_wrapper.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Dom\import_simplexml() refuses a node that already carries a legacy object
--EXTENSIONS--
dom
simplexml
--FILE--
<?php

// Reached through a genuine SimpleXMLElement: the node already carries a
// DOMElement, so returning it would violate the Attr|Element return type.
$doc = new DOMDocument;
$doc->loadXML('<r z="3"><a/></r>');
$element = $doc->documentElement;
$sxe = simplexml_import_dom($element);
try {
Dom\import_simplexml($sxe);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

// Same for an attribute node, which SimpleXML refuses to import, so it is
// passed directly.
$attr = $doc->documentElement->getAttributeNode('z');
try {
Dom\import_simplexml($attr);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}

// The document keeps its model: it must not have been relabelled on the way out.
var_dump(get_class($doc->createElement('z')));

// A node with no legacy object attached is still imported normally.
$clean = simplexml_load_string('<r/>');
var_dump(get_class(Dom\import_simplexml($clean)));

?>
--EXPECT--
Dom\import_simplexml(): Argument #1 ($node) must not be already imported as a DOMNode
Dom\import_simplexml(): Argument #1 ($node) must not be already imported as a DOMNode
string(10) "DOMElement"
string(11) "Dom\Element"
Loading