colder                                   Thu, 08 Sep 2011 15:52:59 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=316420

Log:
SplFileObject does not accept directories. It did not check for that and 
blindly opened the directory, which works on linux but fails on windows. Now 
SplFileObject uses a stat() call to make sure it isn't a directory, if it is, 
it fails with an exception. Tests have been changed accordingly.

Changed paths:
    U   php/php-src/branches/PHP_5_4/ext/spl/spl_directory.c
    U   
php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_fflush_basic_001.phpt
    U   
php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt
    U   php/php-src/branches/PHP_5_4/ext/spl/tests/fileobject_003.phpt
    U   php/php-src/trunk/ext/spl/spl_directory.c
    U   php/php-src/trunk/ext/spl/tests/SplFileObject_fflush_basic_001.phpt
    U   php/php-src/trunk/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt
    U   php/php-src/trunk/ext/spl/tests/fileobject_003.phpt

Modified: php/php-src/branches/PHP_5_4/ext/spl/spl_directory.c
===================================================================
--- php/php-src/branches/PHP_5_4/ext/spl/spl_directory.c	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/branches/PHP_5_4/ext/spl/spl_directory.c	2011-09-08 15:52:59 UTC (rev 316420)
@@ -2222,6 +2222,7 @@
 	char *p1, *p2;
 	char *tmp_path;
 	int   tmp_path_len;
+	zval  tmp;
 	zend_error_handling error_handling;

 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
@@ -2243,6 +2244,16 @@
 		intern->u.file.open_mode = "r";
 		intern->u.file.open_mode_len = 1;
 	}
+
+	php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, &tmp TSRMLS_CC);
+
+	if (Z_LVAL(tmp)) {
+		zend_restore_error_handling(&error_handling TSRMLS_CC);
+		intern->u.file.open_mode = NULL;
+		intern->file_name = NULL;
+		zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Cannot use SplFileObject with directories");
+		return;
+	}

 	if (spl_filesystem_file_open(intern, use_include_path, 0 TSRMLS_CC) == SUCCESS) {
 		tmp_path_len = strlen(intern->u.file.stream->orig_path);

Modified: php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_fflush_basic_001.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_fflush_basic_001.phpt	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_fflush_basic_001.phpt	2011-09-08 15:52:59 UTC (rev 316420)
@@ -17,9 +17,12 @@
     var $varname;

     function stream_open($path, $mode, $options, &$opened_path)
-    {
+    {
         return true;
     }
+
+    function url_stat() {
+    }
 }
 stream_wrapper_register("SPLtest", "VariableStream");
 $ftruncate_test = "";

Modified: php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/branches/PHP_5_4/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt	2011-09-08 15:52:59 UTC (rev 316420)
@@ -12,6 +12,9 @@
     {
         return true;
     }
+
+    function url_stat() {
+    }
 }
 stream_wrapper_register("SPLtest", "VariableStream");
 $ftruncate_test = "";

Modified: php/php-src/branches/PHP_5_4/ext/spl/tests/fileobject_003.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/spl/tests/fileobject_003.phpt	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/branches/PHP_5_4/ext/spl/tests/fileobject_003.phpt	2011-09-08 15:52:59 UTC (rev 316420)
@@ -18,18 +18,21 @@
 	var_dump($o == $c);
 	var_dump($o->getPathname() == $c->getPathname());

-	$f = new SplFileObject($name);
-	var_dump($name);
-	var_dump($f->getPathName());
-	$l = substr($f->getPathName(), -1);
-	var_dump($l != '/' && $l != '\\' && $l == $lc);
-	var_dump($f->getFileName());
-	$l = substr($f->getFileName(), -1);
-	var_dump($l != '/' && $l != '\\' && $l == $lc);
-	var_dump($f->getPath());
-	$l = substr($f->getPath(), -1);
-	var_dump($l != '/' && $l != '\\' && $l == $lp);
-
+	try {
+		$f = new SplFileObject($name);
+		var_dump($name);
+		var_dump($f->getPathName());
+		$l = substr($f->getPathName(), -1);
+		var_dump($l != '/' && $l != '\\' && $l == $lc);
+		var_dump($f->getFileName());
+		$l = substr($f->getFileName(), -1);
+		var_dump($l != '/' && $l != '\\' && $l == $lc);
+		var_dump($f->getPath());
+		$l = substr($f->getPath(), -1);
+		var_dump($l != '/' && $l != '\\' && $l == $lp);
+	} catch (LogicException $e) {
+		echo "LogicException: ".$e->getMessage()."\n";
+	}
 	$fo = $o->openFile();
 	var_dump($fo->getPathName(), $fo->getFileName(), $fo->getPath());
 }
@@ -84,14 +87,8 @@
 bool(false)
 bool(true)
 bool(true)
-%s(%d) "%stests/"
+LogicException: Cannot use SplFileObject with directories
 string(%d) "%stests"
-bool(true)
-string(5) "tests"
-bool(true)
-string(%d) "%sspl"
-bool(true)
-string(%d) "%stests"
 string(%d) "tests"
 string(%d) "%sspl"
 ===2===
@@ -110,14 +107,8 @@
 bool(false)
 bool(true)
 bool(true)
-%s(%d) "%stests"
+LogicException: Cannot use SplFileObject with directories
 string(%d) "%stests"
-bool(true)
-string(%d) "tests"
-bool(true)
-string(%d) "%sspl"
-bool(true)
-string(%d) "%stests"
 string(5) "tests"
 string(%d) "%sspl"
 ===DONE===

Modified: php/php-src/trunk/ext/spl/spl_directory.c
===================================================================
--- php/php-src/trunk/ext/spl/spl_directory.c	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/trunk/ext/spl/spl_directory.c	2011-09-08 15:52:59 UTC (rev 316420)
@@ -2223,6 +2223,7 @@
 	char *p1, *p2;
 	char *tmp_path;
 	int   tmp_path_len;
+	zval  tmp;
 	zend_error_handling error_handling;

 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
@@ -2244,6 +2245,16 @@
 		intern->u.file.open_mode = "r";
 		intern->u.file.open_mode_len = 1;
 	}
+
+	php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, &tmp TSRMLS_CC);
+
+	if (Z_LVAL(tmp)) {
+		zend_restore_error_handling(&error_handling TSRMLS_CC);
+		intern->u.file.open_mode = NULL;
+		intern->file_name = NULL;
+		zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Cannot use SplFileObject with directories");
+		return;
+	}

 	if (spl_filesystem_file_open(intern, use_include_path, 0 TSRMLS_CC) == SUCCESS) {
 		tmp_path_len = strlen(intern->u.file.stream->orig_path);

Modified: php/php-src/trunk/ext/spl/tests/SplFileObject_fflush_basic_001.phpt
===================================================================
--- php/php-src/trunk/ext/spl/tests/SplFileObject_fflush_basic_001.phpt	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/trunk/ext/spl/tests/SplFileObject_fflush_basic_001.phpt	2011-09-08 15:52:59 UTC (rev 316420)
@@ -17,9 +17,12 @@
     var $varname;

     function stream_open($path, $mode, $options, &$opened_path)
-    {
+    {
         return true;
     }
+
+    function url_stat() {
+    }
 }
 stream_wrapper_register("SPLtest", "VariableStream");
 $ftruncate_test = "";

Modified: php/php-src/trunk/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt
===================================================================
--- php/php-src/trunk/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/trunk/ext/spl/tests/SplFileObject_ftruncate_error_001.phpt	2011-09-08 15:52:59 UTC (rev 316420)
@@ -12,6 +12,9 @@
     {
         return true;
     }
+
+    function url_stat() {
+    }
 }
 stream_wrapper_register("SPLtest", "VariableStream");
 $ftruncate_test = "";

Modified: php/php-src/trunk/ext/spl/tests/fileobject_003.phpt
===================================================================
--- php/php-src/trunk/ext/spl/tests/fileobject_003.phpt	2011-09-08 15:20:17 UTC (rev 316419)
+++ php/php-src/trunk/ext/spl/tests/fileobject_003.phpt	2011-09-08 15:52:59 UTC (rev 316420)
@@ -18,18 +18,21 @@
 	var_dump($o == $c);
 	var_dump($o->getPathname() == $c->getPathname());

-	$f = new SplFileObject($name);
-	var_dump($name);
-	var_dump($f->getPathName());
-	$l = substr($f->getPathName(), -1);
-	var_dump($l != '/' && $l != '\\' && $l == $lc);
-	var_dump($f->getFileName());
-	$l = substr($f->getFileName(), -1);
-	var_dump($l != '/' && $l != '\\' && $l == $lc);
-	var_dump($f->getPath());
-	$l = substr($f->getPath(), -1);
-	var_dump($l != '/' && $l != '\\' && $l == $lp);
-
+	try {
+		$f = new SplFileObject($name);
+		var_dump($name);
+		var_dump($f->getPathName());
+		$l = substr($f->getPathName(), -1);
+		var_dump($l != '/' && $l != '\\' && $l == $lc);
+		var_dump($f->getFileName());
+		$l = substr($f->getFileName(), -1);
+		var_dump($l != '/' && $l != '\\' && $l == $lc);
+		var_dump($f->getPath());
+		$l = substr($f->getPath(), -1);
+		var_dump($l != '/' && $l != '\\' && $l == $lp);
+	} catch (LogicException $e) {
+		echo "LogicException: ".$e->getMessage()."\n";
+	}
 	$fo = $o->openFile();
 	var_dump($fo->getPathName(), $fo->getFileName(), $fo->getPath());
 }
@@ -84,14 +87,8 @@
 bool(false)
 bool(true)
 bool(true)
-%s(%d) "%stests/"
+LogicException: Cannot use SplFileObject with directories
 string(%d) "%stests"
-bool(true)
-string(5) "tests"
-bool(true)
-string(%d) "%sspl"
-bool(true)
-string(%d) "%stests"
 string(%d) "tests"
 string(%d) "%sspl"
 ===2===
@@ -110,14 +107,8 @@
 bool(false)
 bool(true)
 bool(true)
-%s(%d) "%stests"
+LogicException: Cannot use SplFileObject with directories
 string(%d) "%stests"
-bool(true)
-string(%d) "tests"
-bool(true)
-string(%d) "%sspl"
-bool(true)
-string(%d) "%stests"
 string(5) "tests"
 string(%d) "%sspl"
 ===DONE===
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to