Commit ebfad221fb9 for php

commit ebfad221fb91123b48e8ddd13b9e161c7b815ec8
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date:   Thu Nov 19 08:36:38 2026 -0500

    ext/dom: Fix crash in named node map iterator key() without a current item

    php_dom_iterator_current_key() read the object out of iterator->curobj
    for attribute and DTD named node maps without checking that the iterator
    has a current item. On an empty attribute map the object pointer is NULL
    and InternalIterator::key() crashes; once the iterator has moved past the
    last item it reads the wrapper move_forward() has already released.
    Return null in that case, as valid() and current() already treat an
    undefined curobj as the end. An entity or notation map of a DTD without
    an internal subset has no hash table and was keyed like a node list,
    returning int(0); select the name-keyed maps by node type instead, as
    move_forward() does. Integer-keyed node lists are unaffected.

    Closes GH-25595

diff --git a/NEWS b/NEWS
index c8a9bdcdf05..dd7e8988250 100644
--- a/NEWS
+++ b/NEWS
@@ -55,6 +55,8 @@ PHP                                                                        NEWS
     evaluation Error instead. (Ilia Alshanetsky)
   . Fixed bug GH-25501 (querySelector() rejects a selector whose identifier
     starts with a non-ASCII character). (David Carlier)
+  . Fixed a crash and a use-after-free in key() of an attribute or DTD named
+    node map iterator that has no current item. (Ilia Alshanetsky)

 - Filter:
   . Fixed stack overflow in filter_var() and filter_var_array() with deeply
diff --git a/ext/dom/dom_iterators.c b/ext/dom/dom_iterators.c
index 5b9051f49b1..d2a9bfb5486 100644
--- a/ext/dom/dom_iterators.c
+++ b/ext/dom/dom_iterators.c
@@ -131,8 +131,10 @@ static void php_dom_iterator_current_key(zend_object_iterator *iter, zval *key)

 	/* Only dtd named node maps, i.e. the ones based on a libxml hash table or attribute collections,
 	 * are keyed by the name because in that case the name is unique. */
-	if (!objmap->ht && objmap->nodetype != XML_ATTRIBUTE_NODE) {
+	if (objmap->nodetype != XML_ATTRIBUTE_NODE && objmap->nodetype != XML_ENTITY_NODE && objmap->nodetype != XML_NOTATION_NODE) {
 		ZVAL_LONG(key, iterator->index);
+	} else if (Z_TYPE(iterator->curobj) != IS_OBJECT) {
+		ZVAL_NULL(key);
 	} else {
 		dom_object *intern = Z_DOMOBJ_P(&iterator->curobj);

diff --git a/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt
new file mode 100644
index 00000000000..b3a333b8da7
--- /dev/null
+++ b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt
@@ -0,0 +1,28 @@
+--TEST--
+DOMNamedNodeMap iterator key() on an empty attribute map
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+$doc = new DOMDocument();
+$doc->loadXML('<root/>');
+$it = $doc->documentElement->attributes->getIterator();
+echo "DOMNamedNodeMap valid: ";
+var_dump($it->valid());
+echo "DOMNamedNodeMap key: ";
+var_dump($it->key());
+echo "DOMNamedNodeMap current: ";
+var_dump($it->current());
+
+$doc = Dom\XMLDocument::createFromString('<root/>');
+$it = $doc->documentElement->attributes->getIterator();
+echo "Dom\NamedNodeMap key: ";
+var_dump($it->key());
+
+?>
+--EXPECT--
+DOMNamedNodeMap valid: bool(false)
+DOMNamedNodeMap key: NULL
+DOMNamedNodeMap current: NULL
+Dom\NamedNodeMap key: NULL
diff --git a/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt
new file mode 100644
index 00000000000..fbd853ee65b
--- /dev/null
+++ b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt
@@ -0,0 +1,34 @@
+--TEST--
+DOMNamedNodeMap iterator key() on an empty DTD named node map
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+$xml = '<!DOCTYPE root><root/>';
+
+$doc = new DOMDocument();
+$doc->loadXML($xml);
+echo "DOMNamedNodeMap entities valid: ";
+var_dump($doc->doctype->entities->getIterator()->valid());
+echo "DOMNamedNodeMap entities key: ";
+var_dump($doc->doctype->entities->getIterator()->key());
+echo "DOMNamedNodeMap notations key: ";
+var_dump($doc->doctype->notations->getIterator()->key());
+
+$doc = Dom\XMLDocument::createFromString($xml);
+echo "Dom\DtdNamedNodeMap entities valid: ";
+var_dump($doc->doctype->entities->getIterator()->valid());
+echo "Dom\DtdNamedNodeMap entities key: ";
+var_dump($doc->doctype->entities->getIterator()->key());
+echo "Dom\DtdNamedNodeMap notations key: ";
+var_dump($doc->doctype->notations->getIterator()->key());
+
+?>
+--EXPECT--
+DOMNamedNodeMap entities valid: bool(false)
+DOMNamedNodeMap entities key: NULL
+DOMNamedNodeMap notations key: NULL
+Dom\DtdNamedNodeMap entities valid: bool(false)
+Dom\DtdNamedNodeMap entities key: NULL
+Dom\DtdNamedNodeMap notations key: NULL
diff --git a/ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt b/ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt
new file mode 100644
index 00000000000..bb256a3156f
--- /dev/null
+++ b/ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt
@@ -0,0 +1,66 @@
+--TEST--
+DOMNamedNodeMap iterator key() after the last item
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+$doc = new DOMDocument();
+$doc->loadXML('<!DOCTYPE root [<!ENTITY e "x"><!NOTATION n SYSTEM "n">]><root a="1"/>');
+$it = $doc->documentElement->attributes->getIterator();
+echo "DOMNamedNodeMap attributes first key: ";
+var_dump($it->key());
+$it->next();
+echo "DOMNamedNodeMap attributes key after end: ";
+var_dump($it->key());
+
+$it = $doc->doctype->entities->getIterator();
+echo "DOMNamedNodeMap entities first key: ";
+var_dump($it->key());
+$it->next();
+echo "DOMNamedNodeMap entities key after end: ";
+var_dump($it->key());
+
+$it = $doc->doctype->notations->getIterator();
+echo "DOMNamedNodeMap notations first key: ";
+var_dump($it->key());
+$it->next();
+echo "DOMNamedNodeMap notations key after end: ";
+var_dump($it->key());
+
+$doc = Dom\XMLDocument::createFromString('<!DOCTYPE root [<!ENTITY e "x"><!NOTATION n SYSTEM "n">]><root a="1"/>');
+$it = $doc->documentElement->attributes->getIterator();
+echo "Dom\NamedNodeMap first key: ";
+var_dump($it->key());
+$it->next();
+echo "Dom\NamedNodeMap key after end: ";
+var_dump($it->key());
+
+$it = $doc->doctype->entities->getIterator();
+echo "Dom\DtdNamedNodeMap entities first key: ";
+var_dump($it->key());
+$it->next();
+echo "Dom\DtdNamedNodeMap entities key after end: ";
+var_dump($it->key());
+
+$it = $doc->doctype->notations->getIterator();
+echo "Dom\DtdNamedNodeMap notations first key: ";
+var_dump($it->key());
+$it->next();
+echo "Dom\DtdNamedNodeMap notations key after end: ";
+var_dump($it->key());
+
+?>
+--EXPECT--
+DOMNamedNodeMap attributes first key: string(1) "a"
+DOMNamedNodeMap attributes key after end: NULL
+DOMNamedNodeMap entities first key: string(1) "e"
+DOMNamedNodeMap entities key after end: NULL
+DOMNamedNodeMap notations first key: string(1) "n"
+DOMNamedNodeMap notations key after end: NULL
+Dom\NamedNodeMap first key: string(1) "a"
+Dom\NamedNodeMap key after end: NULL
+Dom\DtdNamedNodeMap entities first key: string(1) "e"
+Dom\DtdNamedNodeMap entities key after end: NULL
+Dom\DtdNamedNodeMap notations first key: string(1) "n"
+Dom\DtdNamedNodeMap notations key after end: NULL