Skip to content

Commit 70501b8

Browse files
committed
Fixed bug #79852
1 parent 8c89f23 commit 70501b8

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ PHP NEWS
1111
(Nikita)
1212
. Fixed bug #79828 (Segfault when trying to access non-existing variable).
1313
(Nikita)
14+
. Fixed bug #79852 (count(DOMNodeList) doesn't match
15+
count(IteratorIterator(DOMNodeList))). (Nikita)
1416

1517
09 Jul 2020, PHP 8.0.0alpha2
1618

Zend/zend_interfaces.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ ZEND_API int zend_create_internal_iterator_zval(zval *return_value, zval *obj) {
469469
zend_internal_iterator *intern =
470470
(zend_internal_iterator *) zend_internal_iterator_create(zend_ce_internal_iterator);
471471
intern->iter = iter;
472+
intern->iter->index = 0;
472473
ZVAL_OBJ(return_value, &intern->std);
473474
return SUCCESS;
474475
}
@@ -559,8 +560,9 @@ ZEND_METHOD(InternalIterator, next) {
559560
RETURN_THROWS();
560561
}
561562

562-
intern->iter->funcs->move_forward(intern->iter);
563+
/* Advance index first to match foreach behavior. */
563564
intern->iter->index++;
565+
intern->iter->funcs->move_forward(intern->iter);
564566
}
565567

566568
ZEND_METHOD(InternalIterator, valid) {

ext/dom/tests/bug79852.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Bug #79852: count(DOMNodeList) doesn't match count(IteratorIterator(DOMNodeList))
3+
--FILE--
4+
<?php
5+
6+
$XML = <<< XML
7+
<root>
8+
<item>1</item>
9+
<item>2</item>
10+
<item>3</item>
11+
</root>
12+
XML;
13+
14+
$dom = new DomDocument();
15+
$dom->loadXml($XML);
16+
$items = $dom->getElementsByTagName('item');
17+
18+
echo "Count: ".count($items)."\n";
19+
echo "Count: ".iterator_count($items->getIterator())."\n";
20+
$it = new IteratorIterator($items);
21+
echo "Count: ".iterator_count($it)."\n";
22+
echo "Count: ".iterator_count($it)."\n";
23+
24+
?>
25+
--EXPECTF--
26+
Count: 3
27+
Count: 3
28+
Count: 3
29+
30+
Fatal error: Uncaught Error: Iterator does not support rewinding in %s:%d
31+
Stack trace:
32+
#0 [internal function]: InternalIterator->rewind()
33+
#1 [internal function]: IteratorIterator->rewind()
34+
#2 %s(%d): iterator_count(Object(IteratorIterator))
35+
#3 {main}
36+
thrown in %s on line %d

0 commit comments

Comments
 (0)