Commit 613eb791e65 for php

commit 613eb791e6570d5a988b915067422dbe9cb62308
Author: lazerg <lazerg2@gmail.com>
Date:   Sun Dec 6 20:14:35 2026 +0000

    ext/dom: insertAdjacentElement() crash when inserting the element itself

    For beforebegin and afterend, dom_insert_adjacent() adopts the node before
    reading thisp->parent. Adopting a node into its own document unlinks it, so
    when the element is passed as its own argument, the parent becomes NULL and
    php_dom_pre_insert() dereferences it. Adoption is now skipped in that case.
    php_dom_pre_insert() already handles a reference child that is the node
    itself, so the call becomes a no-op as the spec describes.

    Fix GH-26108
    Close GH-26114

diff --git a/NEWS b/NEWS
index 8d12b3e0145..b38e0a698d7 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,8 @@ PHP                                                                        NEWS
     Dom\HTMLDocument creates attributes without a name). (lazerg)
   . Fixed bug GH-26111 (Dom\Document::adoptNode() of a DocumentFragment leaves
     its children on the old document's namespaces). (lazerg, joemax-o360)
+  . Fixed bug GH-26108 (insertAdjacentElement() with the element itself as
+    argument segfaults (beforebegin / afterend)). (lazerg)

 - Enchant:
   . Fixed enchant_broker_set_dict_path() and enchant_broker_get_dict_path()
diff --git a/ext/dom/element.c b/ext/dom/element.c
index bba98fba55b..5bf1d994a9c 100644
--- a/ext/dom/element.c
+++ b/ext/dom/element.c
@@ -1604,27 +1604,40 @@ PHP_METHOD(DOMElement, replaceChildren)
 #define INSERT_ADJACENT_RES_SYNTAX_FAILED INSERT_ADJACENT_RES_ADOPT_FAILED
 #define INSERT_ADJACENT_RES_PRE_INSERT_FAILED ((void*) -2)

+static bool dom_insert_adjacent_adopt(xmlNodePtr otherp, dom_object *this_intern, xmlNodePtr thisp, xmlNodePtr parent, xmlNodePtr insertion_point)
+{
+	if (otherp->doc == thisp->doc) {
+		/* php_dom_pre_insert() unlinks the node, but only after validating. */
+		php_libxml_invalidate_node_list_cache_from_doc(otherp->doc);
+		return true;
+	}
+	if (!php_dom_pre_insert_is_valid_before_adopt(this_intern->document, otherp, parent, insertion_point)) {
+		return false;
+	}
+	return php_dom_adopt_node(otherp, this_intern, thisp->doc);
+}
+
 static xmlNodePtr dom_insert_adjacent(const zend_string *where, xmlNodePtr thisp, dom_object *this_intern, xmlNodePtr otherp)
 {
 	if (zend_string_equals_literal_ci(where, "beforebegin")) {
 		if (thisp->parent == NULL) {
 			return NULL;
 		}
-		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
+		if (!dom_insert_adjacent_adopt(otherp, this_intern, thisp, thisp->parent, thisp)) {
 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
 		}
 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp->parent, thisp)) {
 			return INSERT_ADJACENT_RES_PRE_INSERT_FAILED;
 		}
 	} else if (zend_string_equals_literal_ci(where, "afterbegin")) {
-		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
+		if (!dom_insert_adjacent_adopt(otherp, this_intern, thisp, thisp, thisp->children)) {
 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
 		}
 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp, thisp->children)) {
 			return INSERT_ADJACENT_RES_PRE_INSERT_FAILED;
 		}
 	} else if (zend_string_equals_literal_ci(where, "beforeend")) {
-		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
+		if (!dom_insert_adjacent_adopt(otherp, this_intern, thisp, thisp, NULL)) {
 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
 		}
 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp, NULL)) {
@@ -1634,7 +1647,7 @@ static xmlNodePtr dom_insert_adjacent(const zend_string *where, xmlNodePtr thisp
 		if (thisp->parent == NULL) {
 			return NULL;
 		}
-		if (!php_dom_adopt_node(otherp, this_intern, thisp->doc)) {
+		if (!dom_insert_adjacent_adopt(otherp, this_intern, thisp, thisp->parent, thisp->next)) {
 			return INSERT_ADJACENT_RES_ADOPT_FAILED;
 		}
 		if (!php_dom_pre_insert(this_intern->document, otherp, thisp->parent, thisp->next))  {
diff --git a/ext/dom/parentnode/tree.c b/ext/dom/parentnode/tree.c
index 81e260ee038..bf0519e82bd 100644
--- a/ext/dom/parentnode/tree.c
+++ b/ext/dom/parentnode/tree.c
@@ -573,6 +573,11 @@ bool php_dom_pre_insert(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePt
 	}
 }

+bool php_dom_pre_insert_is_valid_before_adopt(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent, xmlNodePtr insertion_point)
+{
+	return dom_is_pre_insert_valid_without_step_1(document, parent, node, insertion_point, node->doc);
+}
+
 /* https://dom.spec.whatwg.org/#concept-node-append */
 void php_dom_node_append(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent)
 {
diff --git a/ext/dom/php_dom.h b/ext/dom/php_dom.h
index 2e6f6ef7c45..9cbd22e2ede 100644
--- a/ext/dom/php_dom.h
+++ b/ext/dom/php_dom.h
@@ -220,6 +220,7 @@ bool php_dom_fragment_insertion_hierarchy_check_pre_insertion(xmlNodePtr parent,
 bool php_dom_fragment_insertion_hierarchy_check_replace(xmlNodePtr parent, xmlNodePtr node, xmlNodePtr child);
 void php_dom_node_append(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent);
 bool php_dom_pre_insert(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent, xmlNodePtr insertion_point);
+bool php_dom_pre_insert_is_valid_before_adopt(php_libxml_ref_obj *document, xmlNodePtr node, xmlNodePtr parent, xmlNodePtr insertion_point);
 bool php_dom_pre_insert_is_parent_invalid(xmlNodePtr parent);
 void dom_parent_node_query_selector(xmlNodePtr thisp, dom_object *intern, zval *return_value, const zend_string *selectors_str);
 void dom_parent_node_query_selector_all(xmlNodePtr thisp, dom_object *intern, zval *return_value, const zend_string *selectors_str);
diff --git a/ext/dom/tests/gh26108.phpt b/ext/dom/tests/gh26108.phpt
new file mode 100644
index 00000000000..e898f121da5
--- /dev/null
+++ b/ext/dom/tests/gh26108.phpt
@@ -0,0 +1,81 @@
+--TEST--
+GH-26108 (insertAdjacentElement() with the element itself as argument segfaults (beforebegin / afterend))
+--EXTENSIONS--
+dom
+--CREDITS--
+joemax-o360
+--FILE--
+<?php
+$dom = new DOMDocument();
+$dom->loadXML('<r><a/></r>');
+$a = $dom->documentElement->firstChild;
+var_dump($a->insertAdjacentElement('beforebegin', $a) === $a);
+var_dump($a->insertAdjacentElement('afterend', $a) === $a);
+echo $dom->saveXML();
+
+$doc = Dom\XMLDocument::createFromString('<r><x/><a/><y/></r>');
+$a = $doc->documentElement->firstElementChild->nextElementSibling;
+var_dump($a->insertAdjacentElement(Dom\AdjacentPosition::BeforeBegin, $a) === $a);
+var_dump($a->insertAdjacentElement(Dom\AdjacentPosition::AfterEnd, $a) === $a);
+echo $doc->saveXml(), "\n";
+
+try {
+    $doc->documentElement->insertAdjacentElement(Dom\AdjacentPosition::BeforeBegin, $doc->documentElement);
+} catch (DOMException $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+echo $doc->saveXml(), "\n";
+
+$doc = Dom\XMLDocument::createFromString('<r><p><a/></p><q/></r>');
+$a = $doc->documentElement->firstElementChild->firstElementChild;
+try {
+    $a->insertAdjacentElement(Dom\AdjacentPosition::AfterBegin, $a);
+} catch (DOMException $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+try {
+    $a->insertAdjacentElement(Dom\AdjacentPosition::BeforeEnd, $a);
+} catch (DOMException $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+echo $doc->saveXml(), "\n";
+
+$p = $a->parentNode;
+try {
+    $a->insertAdjacentElement(Dom\AdjacentPosition::BeforeBegin, $p);
+} catch (DOMException $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+echo $doc->saveXml(), "\n";
+
+$other = Dom\XMLDocument::createFromString('<o><e/></o>');
+$e = $other->documentElement->firstChild;
+try {
+    $doc->documentElement->insertAdjacentElement(Dom\AdjacentPosition::BeforeBegin, $e);
+} catch (DOMException $ex) {
+    echo $ex::class, ": ", $ex->getMessage(), "\n";
+}
+echo $other->saveXml(), "\n";
+?>
+--EXPECT--
+bool(true)
+bool(true)
+<?xml version="1.0"?>
+<r><a/></r>
+bool(true)
+bool(true)
+<?xml version="1.0" encoding="UTF-8"?>
+<r><x/><a/><y/></r>
+DOMException: Cannot have more than one element child in a document
+<?xml version="1.0" encoding="UTF-8"?>
+<r><x/><a/><y/></r>
+DOMException: Hierarchy Request Error
+DOMException: Hierarchy Request Error
+<?xml version="1.0" encoding="UTF-8"?>
+<r><p><a/></p><q/></r>
+DOMException: Hierarchy Request Error
+<?xml version="1.0" encoding="UTF-8"?>
+<r><p><a/></p><q/></r>
+DOMException: Cannot have more than one element child in a document
+<?xml version="1.0" encoding="UTF-8"?>
+<o><e/></o>