php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78799 Calling iterator_count() is killing the validity of RecursiveDirectoryIterator
Submitted: 2019-11-09 01:46 UTC Modified: 2019-11-09 03:19 UTC
From: k-gun at mail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 7.4.0RC4 OS: Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
10 + 32 = ?
Subscribe to this entry?

 
 [2019-11-09 01:46 UTC] k-gun at mail dot com
Description:
------------
Hi,

Calling iterator_count() is affecting RecursiveDirectoryIterator::valid() and RecursiveDirectoryIterator::valid() is returning false after iterator_count() call.

I am not sure that is a bug or just a behavior of PHP, but simply looks weird invalidating it without any iteration act such as while or foreach.

Test script:
---------------
$iter = new RecursiveDirectoryIterator("/tmp/", RecursiveDirectoryIterator::SKIP_DOTS);
var_dump($iter->valid());
var_dump(iterator_count($iter));
var_dump($iter->valid());

Expected result:
----------------
bool(true)
int(24)
bool(true)

Actual result:
--------------
bool(true)
int(24)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-09 01:48 UTC] k-gun at mail dot com
-: krmgns at gmail dot com +: k-gun at mail dot com -PHP Version: 7.4.0RC5 +PHP Version: 7.4.0RC4
 [2019-11-09 01:48 UTC] k-gun at mail dot com
New email.
 [2019-11-09 03:19 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-11-09 03:19 UTC] requinix@php.net
https://www.php.net/manual/en/function.iterator-count.php
> iterator_count() is not guaranteed to retain the current position of the iterator.

The only way to count how many items there are in an iterator is to run it to the end.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 09:01:31 2024 UTC