Skip to content

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

Closed
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/dom-iterator-key
Closed

iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/dom-iterator-key

Conversation

@iliaal

@iliaal iliaal commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

key() on the iterator of an attribute or DTD named node map reads the current item's object without checking that there is one, so it segfaults on an empty map, such as the attributes of <root/>, and reads a released wrapper once next() has moved past the last item. Return null in that case, as valid() and current() already treat it as the end. The entity and notation maps of a DTD without an internal subset have no hash table and were keyed like a node list, returning int(0) instead of null, so select the name-keyed maps by node type instead.

@devnexen

Copy link
Copy Markdown
Member

can the following test be added ?

--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

… 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.
@iliaal
iliaal force-pushed the fix/dom-iterator-key branch from 9b8724d to 9746e29 Compare November 20, 2026 23:38
@iliaal

iliaal commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Added. It caught a second case: a DTD without an internal subset has no entity or notation hash table, so key() took the node-list branch and returned int(0). Switched the check to the node type.

@iliaal iliaal closed this in ebfad22 Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants