Fix GH-8235: iterator_count() may run indefinitely

We need to prevent integer overflow to eventually stop the iteration.

A test case doesn't appear sensible for this, because even on 32bit
architectures a respective test easily runs for a few minutes.

Closes GH-8447.
This commit is contained in:
Christoph M. Becker 2022-04-27 16:45:03 +02:00
parent 136ef6f129
commit ad7b9f4e50
No known key found for this signature in database
GPG key ID: D66C9593118BCCB6
2 changed files with 6 additions and 0 deletions

3
NEWS
View file

@ -8,6 +8,9 @@ PHP NEWS
- FPM:
. Fixed ACL build check on MacOS. (David Carlier)
- SPL:
. Fixed bug GH-8235 (iterator_count() may run indefinitely). (cmb)
- Zip:
. Fixed type for index in ZipArchive::replaceFile. (Martin Rehberger)

View file

@ -3157,6 +3157,9 @@ PHP_FUNCTION(iterator_to_array)
static int spl_iterator_count_apply(zend_object_iterator *iter, void *puser) /* {{{ */
{
if (UNEXPECTED(*(zend_long*)puser == ZEND_LONG_MAX)) {
return ZEND_HASH_APPLY_STOP;
}
(*(zend_long*)puser)++;
return ZEND_HASH_APPLY_KEEP;
}