PHP :: Bug #49723 :: LimitIterator with empty ArrayIterator
| Bug #49723 | LimitIterator with empty ArrayIterator | ||||
|---|---|---|---|---|---|
| Submitted: | 2009-09-30 13:48 UTC | Modified: | 2010-04-27 09:41 UTC | ||
| From: | goetas at lignano dot it | Assigned: | colder (profile) | ||
| Status: | Closed | Package: | SPL related | ||
| PHP Version: | 5.3.0 | OS: | * | ||
| Private report: | No | CVE-ID: | None | ||
[2009-09-30 13:48 UTC] goetas at lignano dot it
Description:
------------
iterating over an empty seekable iterator causes an unexpected exception.
with actual implementation of LimitIterator::rewind() there is no way to do an empty loop over an empty ArrayIterator.
i suggest this implementation for LimitIterator::rewind() method
function rewind(){
$this->it->rewind();
$this->pos = 0;
if($this->it->valid()){ // check for empty iterators
$this->seek($this->offset);
}
}
Reproduce code:
---------------
$it = new ArrayIterator(array());
$limIt = new LimitIterator($it, 0, 5);
foreach ($limIt as $item){
echo $item;
}
Expected result:
----------------
an empty loop
Actual result:
--------------
Uncaught exception 'OutOfBoundsException' with message 'Seek position 0 is out of range'
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2009-10-02 18:23 UTC] goetas at lignano dot it
[2009-10-02 18:29 UTC] goetas at lignano dot it
source code of LimitIterator, "seek" method at line 63: if ($this->it instanceof SeekableIterator) { $this->it->seek($position); <-- there is no check if i can move pointer to $position like on else branch $this->pos = $position; } else { while($this->pos < $position && $this->it->valid()) { <-- valid() ensures that i can move to $position $this->next(); } }[2009-10-05 23:47 UTC] kalle@php.net
[2009-10-06 15:34 UTC] goetas at lignano dot it
i think this can be a bug... or not correcty explained feature, becasue following code works fine: $it = new EmptyIterator(); // EmptyIterator instead of ArrayIterator $limIt = new LimitIterator($it, 0, 5); foreach ($limIt as $item){ echo $item; } both range are empty, but EmptyIterator does not implements SeekableIterator while ArrayIterator it does. exception is thrown only if iterator implements SeekableIterator and is empty.[2010-04-27 09:41 UTC] colder@php.net
-Status: Assigned +Status: Closed
[2010-04-27 09:41 UTC] colder@php.net