diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2020-07-14 12:30:50 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-07-14 12:31:06 +0200 |
commit | 70501b81a75c7c4cd0ec3a121e290e561829deb9 (patch) | |
tree | decfc1f946fe6b9019ceb0b18f87522b6905ce88 | |
parent | 8c89f23c4552405b4603156c0c81d6cc0bd7f5c3 (diff) | |
download | php-git-70501b81a75c7c4cd0ec3a121e290e561829deb9.tar.gz |
Fixed bug #79852
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | Zend/zend_interfaces.c | 4 | ||||
-rw-r--r-- | ext/dom/tests/bug79852.phpt | 36 |
3 files changed, 41 insertions, 1 deletions
@@ -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 diff --git a/Zend/zend_interfaces.c b/Zend/zend_interfaces.c index 68d0077795..37c72ef554 100644 --- a/Zend/zend_interfaces.c +++ b/Zend/zend_interfaces.c @@ -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; } @@ -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) { diff --git a/ext/dom/tests/bug79852.phpt b/ext/dom/tests/bug79852.phpt new file mode 100644 index 0000000000..67d5edeca3 --- /dev/null +++ b/ext/dom/tests/bug79852.phpt @@ -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 |