php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70561 DirectoryIterator::seek should throw OutOfBoundsException
Submitted: 2015-09-23 14:01 UTC Modified: 2015-10-29 09:56 UTC
Votes:3
Avg. Score:3.7 ± 0.5
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: bishop@php.net Assigned: tyrael (profile)
Status: Closed Package: SPL related
PHP Version: Irrelevant 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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bishop@php.net
New email:
PHP Version: OS:

 

 [2015-09-23 14:01 UTC] bishop@php.net
Description:
------------
Originally reported on SO: http://stackoverflow.com/q/30398762/2908724

DirecotryIterator implements SeekableIterator, which "should throw an OutOfBoundsException if the position is not seekable".  However, the DirectoryIterator implementation simply returns when the position cannot be sought.

This leads to developer confusion, like:

So how would one reasonably expect to know whether to use valid() to confirm valid position after seek($position) while also anticipating that the seek() might throw an Exception instead of updating the position, so that valid() returns true?


Related to https://bugs.php.net/bug.php?id=32130

Test script:
---------------
$dir_iterator = new DirectoryIterator(dirname(__FILE__));
$dir_iterator->seek(1);
try {
    $dir_iterator->seek(500);  // arbitrarily high seek position
} catch (OutOfBoundsException $e) {
    echo $e->getMessage() . PHP_EOL;
}
echo $dir_iterator->key() . PHP_EOL;
echo "Is valid? " . (int) $dir_iterator->valid() . PHP_EOL;


Expected result:
----------------
Seek position 500 is out of range
90
Is valid? 0


Actual result:
--------------
90
Is valid? 0

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-29 09:56 UTC] tyrael@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: tyrael
 [2015-10-29 09:56 UTC] tyrael@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

the fix should be released with 5.6.16
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 14:01:31 2024 UTC