Skip to content
Merged
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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ PHP NEWS
- DOM:
. Fixed bug GH-22570 (Stack overflow when serializing a deeply nested
Dom\XMLDocument). (iliaal)
. Fixed Dom\DtdNamedNodeMap integer dimension access so negative indexes
return NULL and indexes outside the int range throw ValueError instead of
returning the first entity or notation. (Weilin Du)

- Exif:
. Fixed bug GH-11020 (exif_read_data() emits a spurious "Illegal IFD size"
Expand Down
12 changes: 10 additions & 2 deletions ext/dom/dom_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,22 @@ void dom_free_notation(xmlEntityPtr entity) /* {{{ */
}
/* }}} */

xmlNodePtr php_dom_libxml_hash_iter(xmlHashTable *ht, int index)
xmlNodePtr php_dom_libxml_hash_iter(xmlHashTable *ht, zend_long index)
{
int htsize;

if (index < 0) {
return NULL;
}
if (UNEXPECTED(ZEND_LONG_INT_OVFL(index))) {
zend_value_error("must be between 0 and %d", INT_MAX);
return NULL;
}

if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
nodeIterator iter;
iter.cur = 0;
iter.index = index;
iter.index = (int) index;
iter.node = NULL;
xmlHashScan(ht, itemHashScanner, &iter);
return iter.node;
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ bool dom_node_is_read_only(const xmlNode *node);
bool dom_node_children_valid(const xmlNode *node);
xmlNodePtr create_notation(xmlDtdPtr parent_dtd, const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
void dom_free_notation(xmlEntityPtr entity);
xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index);
xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, zend_long index);
zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
void dom_set_doc_classmap(php_libxml_ref_obj *document, zend_class_entry *basece, zend_class_entry *ce);
xmlNodePtr php_dom_create_fake_namespace_decl(xmlNodePtr nodep, xmlNsPtr original, zval *return_value, dom_object *parent_intern);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Dom\DtdNamedNodeMap dimension access handles invalid integer indexes
--EXTENSIONS--
dom
--SKIPIF--
<?php
if (PHP_INT_SIZE == 4) {
die("skip this test is for 64bit platform only");
}
?>
--FILE--
<?php
$doc = Dom\XMLDocument::createFromString(<<<XML
<!DOCTYPE root [
<!ENTITY e "entity">
<!NOTATION n SYSTEM "notation">
]>
<root/>
XML);

$overflow = 4294967296;

function dump_access(Closure $callback): void {
try {
var_dump($callback()?->nodeName);
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
}

dump_access(fn() => $doc->doctype->entities[-1]);
dump_access(fn() => $doc->doctype->entities[$overflow]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let s test the negative values too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given other modern DOM collection dimension returns None in negative indexes and ValueError in out-of-range indexes, let's align with them.

$element->attributes[-1]; // NULL
$doctype->entities->item(4294967296); // ValueError: Argument #1 ($index) must be between 0 and 2147483647


dump_access(fn() => $doc->doctype->notations[-1]);
dump_access(fn() => $doc->doctype->notations[$overflow]);
?>
--EXPECT--
NULL
must be between 0 and 2147483647
NULL
must be between 0 and 2147483647
Loading