php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49607 CachingIterator & DirectoryIterator incorrect behaviour
Submitted: 2009-09-20 14:18 UTC Modified: 2018-08-17 18:07 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: david at grudl dot com Assigned:
Status: Verified Package: SPL related
PHP Version: 5.3.0 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: david at grudl dot com
New email:
PHP Version: OS:

 

 [2009-09-20 14:18 UTC] david at grudl dot com
Description:
------------
Iteration over CachingIterator with DirectoryIterator leads to incorrect results.


Reproduce code:
---------------
// w/o CachingIterator
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $val) {
	echo $val;
}

// with CachingIterator
$iterator = new CachingIterator($dir);
foreach ($iterator as $val) {
	echo $val;
}



Expected result:
----------------
It is expected the both code snippets produces the same result:

.
..
anyfile.php

Actual result:
--------------
// w/o CachingIterator:
.
..
anyfile.php

// with CachingIterator:
..
anyfile.php
(empty string)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-30 20:07 UTC] jani@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See bug #49608
 [2009-12-01 13:17 UTC] david at grudl dot com
Please open this bug, it is something different to 49608. This bug is 
not about crash, it is about incorrect iteration results.
 [2014-07-03 21:39 UTC] vrana@php.net
-Status: Not a bug +Status: Re-Opened
 [2014-07-03 21:39 UTC] vrana@php.net
This is still not fixed.
 [2018-08-17 18:07 UTC] cmb@php.net
-Status: Re-Opened +Status: Verified
 [2018-08-17 18:07 UTC] cmb@php.net
This issue not only affects DirectoryIterators, but rather
all Iterators which return themselves on ::current()[1].

To explain: when a CachingIterator is rewound, it rewinds its
inner iterator, fetches its current value, caches it, and calls
::next() on the inner iterator.  For self-returning Iterators the
cached result is then already the second value.  On
CachingIterator::next() the current value of the inner iterator
cached, and again ::next() is called on the inner iterator …

The erroneous behavior reaches its climax, if
CachingIterator::FULL_CACHE is requested, and after the first
iteration ::getCache() is called: all values are identical.  So to
fix this bug, we'd have to cache clones of the values (and make
sure that all values returned by the Iterator's ::current() would
actually be cloneable).

[1] <https://3v4l.org/dUgEg>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 17:01:30 2024 UTC