]> granicus.if.org Git - php/commitdiff
ValueError for empty path in stream code
authorGeorge Peter Banyard <girgias@php.net>
Tue, 28 Jul 2020 13:50:41 +0000 (14:50 +0100)
committerGeorge Peter Banyard <girgias@php.net>
Fri, 31 Jul 2020 12:39:58 +0000 (13:39 +0100)
Closes GH-5902

13 files changed:
ext/standard/tests/file/file_get_contents_variation8-win32.phpt
ext/standard/tests/file/file_get_contents_variation8.phpt
ext/standard/tests/file/file_put_contents_variation8-win32.phpt
ext/standard/tests/file/file_put_contents_variation8.phpt
ext/standard/tests/file/readfile_error.phpt
ext/standard/tests/file/readfile_variation10-win32.phpt
ext/standard/tests/file/readfile_variation10.phpt
ext/standard/tests/strings/md5_file.phpt
ext/standard/tests/strings/sha1_file.phpt
ext/tidy/tests/019.phpt
ext/zlib/tests/gzfile_variation3.phpt
ext/zlib/tests/readgzfile_variation3.phpt
main/streams/streams.c

index d2e39b56bdc417e401490cea5366d758603095b5..aa57689ad7e1ac751e2f5444fb0149e76bd6a0b4 100644 (file)
@@ -33,12 +33,12 @@ $names_arr = array(
 );
 
 foreach($names_arr as $key =>$value) {
-  echo "\n-- Filename: $key --\n";
-  try {
-    var_dump(file_get_contents($value));
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
-  }
+    echo "\n-- Filename: $key --\n";
+    try {
+        var_dump(file_get_contents($value));
+    } catch (\TypeError|\ValueError $e) {
+        echo get_class($e) . ': ' . $e->getMessage(), "\n";
+    }
 }
 
 ?>
@@ -56,19 +56,13 @@ Warning: file_get_contents(1): Failed to open stream: No such file or directory
 bool(false)
 
 -- Filename: FALSE --
-
-Warning: file_get_contents(): Filename cannot be empty in %s on line %d
-bool(false)
+ValueError: Path cannot be empty
 
 -- Filename: NULL --
-
-Warning: file_get_contents(): Filename cannot be empty in %sfile_get_contents_variation8-win32.php on line %d
-bool(false)
+ValueError: Path cannot be empty
 
 -- Filename: "" --
-
-Warning: file_get_contents(): Filename cannot be empty in %s on line %d
-bool(false)
+ValueError: Path cannot be empty
 
 -- Filename: " " --
 
@@ -76,10 +70,10 @@ Warning: file_get_contents( ): Failed to open stream: Permission denied in %s on
 bool(false)
 
 -- Filename: \0 --
-file_get_contents(): Argument #1 ($filename) must be a valid path, string given
+TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, string given
 
 -- Filename: array() --
-file_get_contents(): Argument #1 ($filename) must be a valid path, array given
+TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, array given
 
 -- Filename: /no/such/file/dir --
 
index fb867e28f74f8fdd269436f84221c4e4309ce6c9..5c466f1b206cd986d11065e8e1fc4b58ed3fa227 100644 (file)
@@ -32,12 +32,12 @@ $names_arr = array(
 );
 
 for( $i=0; $i<count($names_arr); $i++ ) {
-  echo "-- Iteration $i --\n";
-  try {
-    var_dump(file_get_contents($names_arr[$i]));
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
-  }
+    echo "-- Iteration $i --\n";
+    try {
+        var_dump(file_get_contents($names_arr[$i]));
+    } catch (\TypeError|\ValueError $e) {
+        echo get_class($e) . ': ' . $e->getMessage(), "\n";
+    }
 }
 
 echo "\n*** Done ***\n";
@@ -53,25 +53,19 @@ bool(false)
 Warning: file_get_contents(1): Failed to open stream: No such file or directory in %s on line %d
 bool(false)
 -- Iteration 2 --
-
-Warning: file_get_contents(): Filename cannot be empty in %s on line %d
-bool(false)
+ValueError: Path cannot be empty
 -- Iteration 3 --
-
-Warning: file_get_contents(): Filename cannot be empty in %s on line %d
-bool(false)
+ValueError: Path cannot be empty
 -- Iteration 4 --
-
-Warning: file_get_contents(): Filename cannot be empty in %s on line %d
-bool(false)
+ValueError: Path cannot be empty
 -- Iteration 5 --
 
 Warning: file_get_contents( ): Failed to open stream: No such file or directory in %s on line %d
 bool(false)
 -- Iteration 6 --
-file_get_contents(): Argument #1 ($filename) must be a valid path, string given
+TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, string given
 -- Iteration 7 --
-file_get_contents(): Argument #1 ($filename) must be a valid path, array given
+TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, array given
 -- Iteration 8 --
 
 Warning: file_get_contents(/no/such/file/dir): Failed to open stream: No such file or directory in %s on line %d
index 5246a5af23e57de188855f0fd0a53dcfa91f3870..1ba4d9e98fd874c3206facf0750e0345f9dfa7bf 100644 (file)
@@ -31,44 +31,38 @@ $names_arr = array(
 );
 
 foreach($names_arr as $key =>$value) {
-  echo "\n-- Filename: $key --\n";
-  try {
-    $res = file_put_contents($value, "Some data");
-    if ($res !== false && $res != null) {
-       echo "$res bytes written to: $value\n";
-       unlink($value);
-    } else {
-       echo "Failed to write data to: $key\n";
+    echo "\n-- Filename: $key --\n";
+    try {
+        $res = file_put_contents($value, "Some data");
+        if ($res !== false && $res != null) {
+            echo "$res bytes written to: '$value'\n";
+            unlink($value);
+        } else {
+            echo "Failed to write data to: $key\n";
+        }
+    } catch (\TypeError|\ValueError $e) {
+        echo get_class($e) . ': ' . $e->getMessage(), "\n";
     }
-  } catch (TypeError $e) {
-      echo $e->getMessage(), "\n";
-  }
-};
+}
 
 ?>
 --EXPECTF--
 *** Testing file_put_contents() : usage variation ***
 
 -- Filename: -1 --
-9 bytes written to: -1
+9 bytes written to: '-1'
 
 -- Filename: TRUE --
-9 bytes written to: 1
+9 bytes written to: '1'
 
 -- Filename: FALSE --
-
-Warning: file_put_contents(): Filename cannot be empty in %s on line %d
-Failed to write data to: FALSE
+ValueError: Path cannot be empty
 
 -- Filename: NULL --
-
-Warning: file_put_contents(): Filename cannot be empty in %s on line %d
-Failed to write data to: NULL
+ValueError: Path cannot be empty
 
 -- Filename: "" --
-
-Warning: file_put_contents(): Filename cannot be empty in %s on line %d
-Failed to write data to: ""
+ValueError: Path cannot be empty
 
 -- Filename: " " --
 
@@ -76,10 +70,10 @@ Warning: file_put_contents( ): Failed to open stream: Permission denied in %s on
 Failed to write data to: " "
 
 -- Filename: \0 --
-file_put_contents(): Argument #1 ($filename) must be a valid path, string given
+TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, string given
 
 -- Filename: array() --
-file_put_contents(): Argument #1 ($filename) must be a valid path, array given
+TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, array given
 
 -- Filename: /no/such/file/dir --
 
index 63ac4b56fd31b99bbada5b7d8381ca860ae95ab0..70e8ef48e5b4903c8e476db42ec6aeb0761a5f18 100644 (file)
@@ -36,19 +36,18 @@ $names_arr = array(
 );
 
 for( $i=0; $i<count($names_arr); $i++ ) {
-  echo "-- Iteration $i --\n";
-  try {
-    $res = file_put_contents($names_arr[$i], "Some data");
-    if ($res !== false && $res != null) {
-       echo "$res bytes written to: $names_arr[$i]\n";
-       unlink($names_arr[$i]);
+    echo "-- Iteration $i --\n";
+    try {
+        $res = file_put_contents($names_arr[$i], "Some data");
+        if ($res !== false && $res != null) {
+            echo "$res bytes written to: '$names_arr[$i]'\n";
+            unlink($names_arr[$i]);
+        } else {
+            echo "Failed to write data to: '$names_arr[$i]'\n";
+        }
+    } catch (\TypeError|\ValueError $e) {
+        echo get_class($e) . ': ' . $e->getMessage(), "\n";
     }
-    else {
-       echo "Failed to write data to: $names_arr[$i]\n";
-    }
-  } catch (TypeError $e) {
-    echo $e->getMessage(), "\n";
-  }
 }
 rmdir($dir);
 
@@ -57,34 +56,28 @@ echo "\n*** Done ***\n";
 --EXPECTF--
 *** Testing file_put_contents() : usage variation ***
 -- Iteration 0 --
-9 bytes written to: -1
+9 bytes written to: '-1'
 -- Iteration 1 --
-9 bytes written to: 1
+9 bytes written to: '1'
 -- Iteration 2 --
-
-Warning: file_put_contents(): Filename cannot be empty in %s on line %d
-Failed to write data to: 
+ValueError: Path cannot be empty
 -- Iteration 3 --
-
-Warning: file_put_contents(): Filename cannot be empty in %s on line %d
-Failed to write data to: 
+ValueError: Path cannot be empty
 -- Iteration 4 --
-
-Warning: file_put_contents(): Filename cannot be empty in %s on line %d
-Failed to write data to: 
+ValueError: Path cannot be empty
 -- Iteration 5 --
-9 bytes written to:  
+9 bytes written to: ' '
 -- Iteration 6 --
-file_put_contents(): Argument #1 ($filename) must be a valid path, string given
+TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, string given
 -- Iteration 7 --
-file_put_contents(): Argument #1 ($filename) must be a valid path, array given
+TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, array given
 -- Iteration 8 --
 
 Warning: file_put_contents(%sdir): Failed to open stream: %s in %s on line %d
-Failed to write data to: %sir
+Failed to write data to: '%sir'
 -- Iteration 9 --
 
 Warning: file_put_contents(%sphp): Failed to open stream: %s in %s on line %d
-Failed to write data to: %sphp
+Failed to write data to: '%sphp'
 
 *** Done ***
index 6ae9f9a4dee123257fd60d29b1c5bd0d10c9ed53..bec5b383d715e03dcddc0f164cbe67f5a6af22d6 100644 (file)
@@ -8,9 +8,21 @@ echo "*** Test readfile(): error conditions ***\n";
 
 echo "\n-- Testing readfile() with invalid arguments --\n";
 // invalid arguments
-var_dump( readfile(NULL) );  // NULL as $filename
-var_dump( readfile('') );  // empty string as $filename
-var_dump( readfile(false) );  // boolean false as $filename
+try {
+    var_dump( readfile(NULL) );  // NULL as $filename
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump( readfile('') );  // empty string as $filename
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump( readfile(false) );  // boolean false as $filename
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Testing readfile() with non-existent file --\n";
 $non_existent_file = __DIR__."/non_existent_file.tmp";
@@ -22,15 +34,9 @@ echo "Done\n";
 *** Test readfile(): error conditions ***
 
 -- Testing readfile() with invalid arguments --
-
-Warning: readfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readfile(): Filename cannot be empty in %s on line %d
-bool(false)
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
 
 -- Testing readfile() with non-existent file --
 
index ecd850eb59d6a8f5a5669e8ba3151d3852955add..05753f93a2df30bc8677802027d95387c2783974 100644 (file)
@@ -35,11 +35,10 @@ foreach($names_arr as $key => $value) {
     echo "\n-- Filename: $key --\n";
     try {
         readfile($value);
-    } catch (TypeError $e) {
-        echo $e->getMessage(), "\n";
+    } catch (\TypeError|\ValueError $e) {
+        echo get_class($e) . ': ' . $e->getMessage(), "\n";
     }
-};
-
+}
 ?>
 --EXPECTF--
 *** Testing readfile() : variation ***
@@ -53,26 +52,23 @@ Warning: readfile(-1): Failed to open stream: No such file or directory in %s on
 Warning: readfile(1): Failed to open stream: No such file or directory in %s on line %d
 
 -- Filename: FALSE --
-
-Warning: readfile(): Filename cannot be empty in %s on line %d
+ValueError: Path cannot be empty
 
 -- Filename: NULL --
-
-Warning: readfile(): Filename cannot be empty in %s on line %d
+ValueError: Path cannot be empty
 
 -- Filename: "" --
-
-Warning: readfile(): Filename cannot be empty in %s on line %d
+ValueError: Path cannot be empty
 
 -- Filename: " " --
 
 Warning: readfile( ): Failed to open stream: Permission denied in %s on line %d
 
 -- Filename: \0 --
-readfile(): Argument #1 ($filename) must be a valid path, string given
+TypeError: readfile(): Argument #1 ($filename) must be a valid path, string given
 
 -- Filename: array() --
-readfile(): Argument #1 ($filename) must be a valid path, array given
+TypeError: readfile(): Argument #1 ($filename) must be a valid path, array given
 
 -- Filename: /no/such/file/dir --
 
index 304f57154c4de6a512856801936480a81f352542..06e010fed07923caf79d5c81706bc722c903d16f 100644 (file)
Binary files a/ext/standard/tests/file/readfile_variation10.phpt and b/ext/standard/tests/file/readfile_variation10.phpt differ
index 4bc909601725157bf4036d5b172cf8ab175d477f..26eb03bc5a5b384f75f9ad1c2f7c656196778f4b 100644 (file)
Binary files a/ext/standard/tests/strings/md5_file.phpt and b/ext/standard/tests/strings/md5_file.phpt differ
index 3d4dbfe2cfbd9c9f633ed4e031992e2eae597adc..55130cb98a7f81ecbe0f53dfa5fb61b39a6df1ca 100644 (file)
@@ -32,7 +32,11 @@ fclose($handle2);
 echo "\n*** Testing for error conditions ***\n";
 
 echo "\n-- No filename --\n";
-var_dump( sha1_file("") );
+try {
+    var_dump( sha1_file("") );
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- invalid filename --\n";
 var_dump( sha1_file("rewncwYcn89q") );
@@ -41,7 +45,11 @@ echo "\n-- Scalar value as filename --\n";
 var_dump( sha1_file(12) );
 
 echo "\n-- NULL as filename --\n";
-var_dump( sha1_file(NULL) );
+try {
+    var_dump( sha1_file(NULL) );
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 echo "\n-- Hexadecimal Output for Empty file as Argument --\n";
 var_dump( sha1_file("EmptyFileSHA1.txt") );
@@ -66,9 +74,7 @@ unlink("EmptyFileSHA1.txt");
 *** Testing for error conditions ***
 
 -- No filename --
-
-Warning: sha1_file(): Filename cannot be empty in %s on line %d
-bool(false)
+Path cannot be empty
 
 -- invalid filename --
 
@@ -81,9 +87,7 @@ Warning: sha1_file(12): Failed to open stream: No such file or directory in %s o
 bool(false)
 
 -- NULL as filename --
-
-Warning: sha1_file(): Filename cannot be empty in %s on line %d
-bool(false)
+Path cannot be empty
 
 -- Hexadecimal Output for Empty file as Argument --
 string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"
index 425b42889d2c0258ded81133be95527320950b49..69d7fd943f213dc867bdc1aaef0adcecb083ec42 100644 (file)
@@ -12,9 +12,18 @@ tidy_repair_string($s, $l, $l);
 tidy_repair_string($s, $s, $s);
 tidy_repair_string($l, $l, $l);
 
-tidy_repair_file($s, $l, $l, $l);
-tidy_repair_file($s, $s, $s, $s);
-tidy_repair_file($l, $l, $l ,$l);
+try {
+    tidy_repair_file($s, $l, $l, $l);
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    tidy_repair_file($s, $s, $s, $s);
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
+tidy_repair_file($l, $l, $l ,$l); // This doesn't emit any warning, TODO look into
 
 echo "Done\n";
 ?>
@@ -28,8 +37,6 @@ Warning: tidy_repair_string(): Could not load configuration file '' in %s on lin
 Warning: tidy_repair_string(): Could not load configuration file '1' in %s on line %d
 
 Warning: tidy_repair_string(): Could not set encoding '1' in %s on line %d
-
-Warning: tidy_repair_file(): Filename cannot be empty in %s on line %d
-
-Warning: tidy_repair_file(): Filename cannot be empty in %s on line %d
+Path cannot be empty
+Path cannot be empty
 Done
index bd6e7a508619e32ee7cc91dea5bb67dee256c850..276c8394688392e3662cb850a2efc50d7ca24777 100644 (file)
@@ -25,24 +25,17 @@ $variation = array(
 
 
 foreach ( $variation as $var ) {
-  var_dump(gzfile( $var ,  $use_include_path ) );
+    try {
+        var_dump(gzfile( $var ,  $use_include_path ) );
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
 }
 ?>
---EXPECTF--
-Warning: gzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: gzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: gzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: gzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: gzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: gzfile(): Filename cannot be empty in %s on line %d
-bool(false)
+--EXPECT--
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
index 11c5933d5209fbdddc9ac9a664f711809c954a39..4a7fcecf08fbb59933cc95d8484101626b5ded91 100644 (file)
@@ -25,24 +25,17 @@ $variation = array(
 
 
 foreach ( $variation as $var ) {
-  var_dump(readgzfile( $var ,  $use_include_path ) );
+    try {
+        var_dump(readgzfile( $var ,  $use_include_path ) );
+    } catch (\ValueError $e) {
+        echo $e->getMessage() . \PHP_EOL;
+    }
 }
 ?>
---EXPECTF--
-Warning: readgzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readgzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readgzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readgzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readgzfile(): Filename cannot be empty in %s on line %d
-bool(false)
-
-Warning: readgzfile(): Filename cannot be empty in %s on line %d
-bool(false)
+--EXPECT--
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
+Path cannot be empty
index ea0738c82a53f54d644517b95557f66c73ce2d41..686427bc96643dc09c74b199dd17560eaea624c9 100644 (file)
@@ -2066,7 +2066,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod
        }
 
        if (!path || !*path) {
-               php_error_docref(NULL, E_WARNING, "Filename cannot be empty");
+               zend_value_error("Path cannot be empty");
                return NULL;
        }