Skip to content
Closed
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ PHP NEWS
. Fixed crash when registerNodeClass() is given a class with a constant
expression that cannot be evaluated; registerNodeClass() now throws the
evaluation Error instead. (Ilia Alshanetsky)
. 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
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/dom_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 28 additions & 0 deletions ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt
Original file line number Diff line number Diff line change
@@ -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
Loading