[php-src] PHP-8.2: Fix crash when calling childNodes next() when iterator is exhausted
| From: | Niels Dossche | Date: | Tue, 30 Apr 2024 20:53:42 +0000 |
| Subject: | [php-src] PHP-8.2: Fix crash when calling childNodes next() when iterator is exhausted | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-128277@lists.php.net to get a copy of this message | ||
Author: Niels Dossche (nielsdos)
Date: 2024-04-30T22:30:58+02:00
Commit: https://github.com/php/php-src/commit/2dbe2d62b3785b26516a6450a74137477b4019e6
Raw diff: https://github.com/php/php-src/commit/2dbe2d62b3785b26516a6450a74137477b4019e6.diff
Fix crash when calling childNodes next() when iterator is exhausted
Closes GH-14091.
Changed paths:
A ext/dom/tests/childNodes_current_crash.phpt
M NEWS
M ext/dom/dom_iterators.c
Diff:
diff --git a/NEWS b/NEWS
index 3a95dec83513..f124c8e4ed54 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,8 @@ PHP NEWS
. Fix crashes when entity declaration is removed while still having entity
references. (nielsdos)
. Fix references not handled correctly in C14N. (nielsdos)
+ . Fix crash when calling childNodes next() when iterator is exhausted.
+ (nielsdos)
09 May 2024, PHP 8.2.19
diff --git a/ext/dom/dom_iterators.c b/ext/dom/dom_iterators.c
index 72c97104db04..670f08a679f2 100644
--- a/ext/dom/dom_iterators.c
+++ b/ext/dom/dom_iterators.c
@@ -147,8 +147,7 @@ static int php_dom_iterator_valid(zend_object_iterator *iter) /* {{{ */
zval *php_dom_iterator_current_data(zend_object_iterator *iter) /* {{{ */
{
php_dom_iterator *iterator = (php_dom_iterator *)iter;
-
- return &iterator->curobj;
+ return Z_ISUNDEF(iterator->curobj) ? NULL : &iterator->curobj;
}
/* }}} */
diff --git a/ext/dom/tests/childNodes_current_crash.phpt
b/ext/dom/tests/childNodes_current_crash.phpt
new file mode 100644
index 000000000000..aa93cf33a648
--- /dev/null
+++ b/ext/dom/tests/childNodes_current_crash.phpt
@@ -0,0 +1,25 @@
+--TEST--
+Crash in childNodes iterator current()
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+$dom = new DOMDocument;
+$dom->loadXML('<foo>foo1</foo>');
+
+$nodes = $dom->documentElement->childNodes;
+$iter = $nodes->getIterator();
+
+var_dump($iter->valid());
+var_dump($iter->current()?->wholeText);
+$iter->next();
+var_dump($iter->valid());
+var_dump($iter->current()?->wholeText);
+
+?>
+--EXPECT--
+bool(true)
+string(4) "foo1"
+bool(false)
+NULL