com php-src: Bug #70561: Fix DirectoryIterator to throw OutOfBoundsException: ext/spl/spl_directory.c ext/spl/tes ts/bug70561.phpt
ext/spl/tests/dit_006. phpt
| From: | Stanislav Malyshev | Date: | Wed, 23 Sep 2015 15:14:52 +0000 |
| Subject: | com php-src: Bug #70561: Fix DirectoryIterator to throw OutOfBoundsException: ext/spl/spl_directory.c ext/spl/tes ts/bug70561.phpt ext/spl/tests/dit_006. phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-89456@lists.php.net to get a copy of this message | ||
Commit: 368d3ff0d95c79ffdf042fb685bda703c7cb5c12
Author: Bishop Bettini <bishop.bettini@gmail.com> Wed, 23 Sep 2015 11:14:52 -0400
Parents: 4c34d574147d2b23d3e4d8fb454b6102eb2ddfc1
Branches: PHP-5.6 PHP-7.0 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=368d3ff0d95c79ffdf042fb685bda703c7cb5c12
Log:
Bug #70561: Fix DirectoryIterator to throw OutOfBoundsException
-------------------------------------------------------------------------------
DirectoryIterator implements SeekableIterator, which "should throw an
OutOfBoundsException if the position is not seekable". As is, seek just returns
and one must call valid(). This approach is different than most (all?) other
SeekableIterator implementations and leads to developer confusion. See the
bug report for a specific example.
Bugs:
https://bugs.php.net/70561
Changed paths:
M ext/spl/spl_directory.c
A ext/spl/tests/bug70561.phpt
M ext/spl/tests/dit_006.phpt
Diff:
diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
index 1d75138..7fb4ecd 100644
--- a/ext/spl/spl_directory.c
+++ b/ext/spl/spl_directory.c
@@ -845,7 +845,8 @@ SPL_METHOD(DirectoryIterator, seek)
retval = NULL;
}
if (!valid) {
- break;
+ zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Seek position %ld is out
of range", pos);
+ return;
}
zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()),
&intern->u.dir.func_next, "next", &retval);
if (retval) {
diff --git a/ext/spl/tests/bug70561.phpt b/ext/spl/tests/bug70561.phpt
new file mode 100644
index 0000000..c6c229a
--- /dev/null
+++ b/ext/spl/tests/bug70561.phpt
@@ -0,0 +1,23 @@
+--TEST--
+Bug #70561 (DirectoryIterator::seek should throw OutOfBoundsException)
+--FILE--
+<?php
+$di = new DirectoryIterator(__DIR__ . '/..');
+
+$cnt = 0;
+$di->rewind();
+while ($di->valid()) {
+ $cnt++;
+ $di->next();
+}
+
+try {
+ $di->seek($cnt+1);
+} catch (OutOfBoundsException $ex) {
+ echo $ex->getMessage() . PHP_EOL;
+}
+echo "Is valid? " . (int) $di->valid() . PHP_EOL;
+?>
+--EXPECTF--
+Seek position %d is out of range
+Is valid? 0
diff --git a/ext/spl/tests/dit_006.phpt b/ext/spl/tests/dit_006.phpt
index 9edbb9f..ed1ceff 100644
--- a/ext/spl/tests/dit_006.phpt
+++ b/ext/spl/tests/dit_006.phpt
@@ -30,11 +30,12 @@ while ($di->valid()) {
echo "Without seek we get $o\n";
-$p = 0;
-$di->seek($o+1);
-while ($di->valid()) {
- $p++;
- $di->next();
+try {
+ $p = 0;
+ $di->seek($o+1);
+ $p = 1;
+} catch (\OutOfBoundsException $ex) {
+ echo $ex->getMessage() . PHP_EOL;
}
var_dump($n !== $m, $m === $o, $p === 0);
@@ -44,6 +45,7 @@ var_dump($n !== $m, $m === $o, $p === 0);
With seek(2) we get %d
With seek(0) we get %d
Without seek we get %d
+Seek position %d is out of range
bool(true)
bool(true)
bool(true)