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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: k-gun at mail dot com
New email:
PHP Version: OS:

 

 [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: Sat Apr 27 19:01:29 2024 UTC