Skip to content

Commit 2dbe2d6

Browse files
committed
Fix crash when calling childNodes next() when iterator is exhausted
Closes GH-14091.
1 parent 30a0b03 commit 2dbe2d6

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ PHP NEWS
66
. Fix crashes when entity declaration is removed while still having entity
77
references. (nielsdos)
88
. Fix references not handled correctly in C14N. (nielsdos)
9+
. Fix crash when calling childNodes next() when iterator is exhausted.
10+
(nielsdos)
911

1012
09 May 2024, PHP 8.2.19
1113

ext/dom/dom_iterators.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ static int php_dom_iterator_valid(zend_object_iterator *iter) /* {{{ */
147147
zval *php_dom_iterator_current_data(zend_object_iterator *iter) /* {{{ */
148148
{
149149
php_dom_iterator *iterator = (php_dom_iterator *)iter;
150-
151-
return &iterator->curobj;
150+
return Z_ISUNDEF(iterator->curobj) ? NULL : &iterator->curobj;
152151
}
153152
/* }}} */
154153

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Crash in childNodes iterator current()
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument;
9+
$dom->loadXML('<foo>foo1</foo>');
10+
11+
$nodes = $dom->documentElement->childNodes;
12+
$iter = $nodes->getIterator();
13+
14+
var_dump($iter->valid());
15+
var_dump($iter->current()?->wholeText);
16+
$iter->next();
17+
var_dump($iter->valid());
18+
var_dump($iter->current()?->wholeText);
19+
20+
?>
21+
--EXPECT--
22+
bool(true)
23+
string(4) "foo1"
24+
bool(false)
25+
NULL

0 commit comments

Comments
 (0)