Skip to content

Commit

Permalink
Fixed bug #79852
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 14, 2020
1 parent 8c89f23 commit 70501b8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PHP NEWS
(Nikita)
. Fixed bug #79828 (Segfault when trying to access non-existing variable).
(Nikita)
. Fixed bug #79852 (count(DOMNodeList) doesn't match
count(IteratorIterator(DOMNodeList))). (Nikita)

09 Jul 2020, PHP 8.0.0alpha2

Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ ZEND_API int zend_create_internal_iterator_zval(zval *return_value, zval *obj) {
zend_internal_iterator *intern =
(zend_internal_iterator *) zend_internal_iterator_create(zend_ce_internal_iterator);
intern->iter = iter;
intern->iter->index = 0;
ZVAL_OBJ(return_value, &intern->std);
return SUCCESS;
}
Expand Down Expand Up @@ -559,8 +560,9 @@ ZEND_METHOD(InternalIterator, next) {
RETURN_THROWS();
}

intern->iter->funcs->move_forward(intern->iter);
/* Advance index first to match foreach behavior. */
intern->iter->index++;
intern->iter->funcs->move_forward(intern->iter);
}

ZEND_METHOD(InternalIterator, valid) {
Expand Down
36 changes: 36 additions & 0 deletions ext/dom/tests/bug79852.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Bug #79852: count(DOMNodeList) doesn't match count(IteratorIterator(DOMNodeList))
--FILE--
<?php

$XML = <<< XML
<root>
<item>1</item>
<item>2</item>
<item>3</item>
</root>
XML;

$dom = new DomDocument();
$dom->loadXml($XML);
$items = $dom->getElementsByTagName('item');

echo "Count: ".count($items)."\n";
echo "Count: ".iterator_count($items->getIterator())."\n";
$it = new IteratorIterator($items);
echo "Count: ".iterator_count($it)."\n";
echo "Count: ".iterator_count($it)."\n";

?>
--EXPECTF--
Count: 3
Count: 3
Count: 3

Fatal error: Uncaught Error: Iterator does not support rewinding in %s:%d
Stack trace:
#0 [internal function]: InternalIterator->rewind()
#1 [internal function]: IteratorIterator->rewind()
#2 %s(%d): iterator_count(Object(IteratorIterator))
#3 {main}
thrown in %s on line %d

0 comments on commit 70501b8

Please sign in to comment.