ValueError for empty path in stream code

Closes GH-5902
This commit is contained in:
George Peter Banyard 2020-07-28 14:50:41 +01:00
parent f78a091014
commit c3105a1f8d
13 changed files with 139 additions and 165 deletions

View File

@ -33,12 +33,12 @@ $names_arr = array(
); );
foreach($names_arr as $key =>$value) { foreach($names_arr as $key =>$value) {
echo "\n-- Filename: $key --\n"; echo "\n-- Filename: $key --\n";
try { try {
var_dump(file_get_contents($value)); var_dump(file_get_contents($value));
} catch (TypeError $e) { } catch (\TypeError|\ValueError $e) {
echo $e->getMessage(), "\n"; 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) bool(false)
-- Filename: FALSE -- -- Filename: FALSE --
ValueError: Path cannot be empty
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
bool(false)
-- Filename: NULL -- -- Filename: NULL --
ValueError: Path cannot be empty
Warning: file_get_contents(): Filename cannot be empty in %sfile_get_contents_variation8-win32.php on line %d
bool(false)
-- Filename: "" -- -- Filename: "" --
ValueError: Path cannot be empty
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
bool(false)
-- Filename: " " -- -- Filename: " " --
@ -76,10 +70,10 @@ Warning: file_get_contents( ): Failed to open stream: Permission denied in %s on
bool(false) bool(false)
-- Filename: \0 -- -- 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() -- -- 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 -- -- Filename: /no/such/file/dir --

View File

@ -32,12 +32,12 @@ $names_arr = array(
); );
for( $i=0; $i<count($names_arr); $i++ ) { for( $i=0; $i<count($names_arr); $i++ ) {
echo "-- Iteration $i --\n"; echo "-- Iteration $i --\n";
try { try {
var_dump(file_get_contents($names_arr[$i])); var_dump(file_get_contents($names_arr[$i]));
} catch (TypeError $e) { } catch (\TypeError|\ValueError $e) {
echo $e->getMessage(), "\n"; echo get_class($e) . ': ' . $e->getMessage(), "\n";
} }
} }
echo "\n*** Done ***\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 Warning: file_get_contents(1): Failed to open stream: No such file or directory in %s on line %d
bool(false) bool(false)
-- Iteration 2 -- -- Iteration 2 --
ValueError: Path cannot be empty
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
bool(false)
-- Iteration 3 -- -- Iteration 3 --
ValueError: Path cannot be empty
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
bool(false)
-- Iteration 4 -- -- Iteration 4 --
ValueError: Path cannot be empty
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
bool(false)
-- Iteration 5 -- -- Iteration 5 --
Warning: file_get_contents( ): Failed to open stream: No such file or directory in %s on line %d Warning: file_get_contents( ): Failed to open stream: No such file or directory in %s on line %d
bool(false) bool(false)
-- Iteration 6 -- -- 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 -- -- 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 -- -- Iteration 8 --
Warning: file_get_contents(/no/such/file/dir): Failed to open stream: No such file or directory in %s on line %d Warning: file_get_contents(/no/such/file/dir): Failed to open stream: No such file or directory in %s on line %d

View File

@ -31,44 +31,38 @@ $names_arr = array(
); );
foreach($names_arr as $key =>$value) { foreach($names_arr as $key =>$value) {
echo "\n-- Filename: $key --\n"; echo "\n-- Filename: $key --\n";
try { try {
$res = file_put_contents($value, "Some data"); $res = file_put_contents($value, "Some data");
if ($res !== false && $res != null) { if ($res !== false && $res != null) {
echo "$res bytes written to: $value\n"; echo "$res bytes written to: '$value'\n";
unlink($value); unlink($value);
} else { } else {
echo "Failed to write data to: $key\n"; 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-- --EXPECTF--
*** Testing file_put_contents() : usage variation *** *** Testing file_put_contents() : usage variation ***
-- Filename: -1 -- -- Filename: -1 --
9 bytes written to: -1 9 bytes written to: '-1'
-- Filename: TRUE -- -- Filename: TRUE --
9 bytes written to: 1 9 bytes written to: '1'
-- Filename: FALSE -- -- Filename: FALSE --
ValueError: Path cannot be empty
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
Failed to write data to: FALSE
-- Filename: NULL -- -- Filename: NULL --
ValueError: Path cannot be empty
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
Failed to write data to: NULL
-- Filename: "" -- -- Filename: "" --
ValueError: Path cannot be empty
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
Failed to write data to: ""
-- Filename: " " -- -- Filename: " " --
@ -76,10 +70,10 @@ Warning: file_put_contents( ): Failed to open stream: Permission denied in %s on
Failed to write data to: " " Failed to write data to: " "
-- Filename: \0 -- -- 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() -- -- 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 -- -- Filename: /no/such/file/dir --

View File

@ -36,19 +36,18 @@ $names_arr = array(
); );
for( $i=0; $i<count($names_arr); $i++ ) { for( $i=0; $i<count($names_arr); $i++ ) {
echo "-- Iteration $i --\n"; echo "-- Iteration $i --\n";
try { try {
$res = file_put_contents($names_arr[$i], "Some data"); $res = file_put_contents($names_arr[$i], "Some data");
if ($res !== false && $res != null) { if ($res !== false && $res != null) {
echo "$res bytes written to: $names_arr[$i]\n"; echo "$res bytes written to: '$names_arr[$i]'\n";
unlink($names_arr[$i]); 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); rmdir($dir);
@ -57,34 +56,28 @@ echo "\n*** Done ***\n";
--EXPECTF-- --EXPECTF--
*** Testing file_put_contents() : usage variation *** *** Testing file_put_contents() : usage variation ***
-- Iteration 0 -- -- Iteration 0 --
9 bytes written to: -1 9 bytes written to: '-1'
-- Iteration 1 -- -- Iteration 1 --
9 bytes written to: 1 9 bytes written to: '1'
-- Iteration 2 -- -- Iteration 2 --
ValueError: Path cannot be empty
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
Failed to write data to:
-- Iteration 3 -- -- Iteration 3 --
ValueError: Path cannot be empty
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
Failed to write data to:
-- Iteration 4 -- -- Iteration 4 --
ValueError: Path cannot be empty
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
Failed to write data to:
-- Iteration 5 -- -- Iteration 5 --
9 bytes written to: 9 bytes written to: ' '
-- Iteration 6 -- -- 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 -- -- 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 -- -- Iteration 8 --
Warning: file_put_contents(%sdir): Failed to open stream: %s in %s on line %d 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 -- -- Iteration 9 --
Warning: file_put_contents(%sphp): Failed to open stream: %s in %s on line %d 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 *** *** Done ***

View File

@ -8,9 +8,21 @@ echo "*** Test readfile(): error conditions ***\n";
echo "\n-- Testing readfile() with invalid arguments --\n"; echo "\n-- Testing readfile() with invalid arguments --\n";
// invalid arguments // invalid arguments
var_dump( readfile(NULL) ); // NULL as $filename try {
var_dump( readfile('') ); // empty string as $filename var_dump( readfile(NULL) ); // NULL as $filename
var_dump( readfile(false) ); // boolean false 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"; echo "\n-- Testing readfile() with non-existent file --\n";
$non_existent_file = __DIR__."/non_existent_file.tmp"; $non_existent_file = __DIR__."/non_existent_file.tmp";
@ -22,15 +34,9 @@ echo "Done\n";
*** Test readfile(): error conditions *** *** Test readfile(): error conditions ***
-- Testing readfile() with invalid arguments -- -- Testing readfile() with invalid arguments --
Path cannot be empty
Warning: readfile(): Filename cannot be empty in %s on line %d Path cannot be empty
bool(false) Path cannot be empty
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)
-- Testing readfile() with non-existent file -- -- Testing readfile() with non-existent file --

View File

@ -35,11 +35,10 @@ foreach($names_arr as $key => $value) {
echo "\n-- Filename: $key --\n"; echo "\n-- Filename: $key --\n";
try { try {
readfile($value); readfile($value);
} catch (TypeError $e) { } catch (\TypeError|\ValueError $e) {
echo $e->getMessage(), "\n"; echo get_class($e) . ': ' . $e->getMessage(), "\n";
} }
}; }
?> ?>
--EXPECTF-- --EXPECTF--
*** Testing readfile() : variation *** *** 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 Warning: readfile(1): Failed to open stream: No such file or directory in %s on line %d
-- Filename: FALSE -- -- Filename: FALSE --
ValueError: Path cannot be empty
Warning: readfile(): Filename cannot be empty in %s on line %d
-- Filename: NULL -- -- Filename: NULL --
ValueError: Path cannot be empty
Warning: readfile(): Filename cannot be empty in %s on line %d
-- Filename: "" -- -- Filename: "" --
ValueError: Path cannot be empty
Warning: readfile(): Filename cannot be empty in %s on line %d
-- Filename: " " -- -- Filename: " " --
Warning: readfile( ): Failed to open stream: Permission denied in %s on line %d Warning: readfile( ): Failed to open stream: Permission denied in %s on line %d
-- Filename: \0 -- -- 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() -- -- 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 -- -- Filename: /no/such/file/dir --

View File

@ -32,7 +32,11 @@ fclose($handle2);
echo "\n*** Testing for error conditions ***\n"; echo "\n*** Testing for error conditions ***\n";
echo "\n-- No filename --\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"; echo "\n-- invalid filename --\n";
var_dump( sha1_file("rewncwYcn89q") ); var_dump( sha1_file("rewncwYcn89q") );
@ -41,7 +45,11 @@ echo "\n-- Scalar value as filename --\n";
var_dump( sha1_file(12) ); var_dump( sha1_file(12) );
echo "\n-- NULL as filename --\n"; 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"; echo "\n-- Hexadecimal Output for Empty file as Argument --\n";
var_dump( sha1_file("EmptyFileSHA1.txt") ); var_dump( sha1_file("EmptyFileSHA1.txt") );
@ -66,9 +74,7 @@ unlink("EmptyFileSHA1.txt");
*** Testing for error conditions *** *** Testing for error conditions ***
-- No filename -- -- No filename --
Path cannot be empty
Warning: sha1_file(): Filename cannot be empty in %s on line %d
bool(false)
-- invalid filename -- -- invalid filename --
@ -81,9 +87,7 @@ Warning: sha1_file(12): Failed to open stream: No such file or directory in %s o
bool(false) bool(false)
-- NULL as filename -- -- NULL as filename --
Path cannot be empty
Warning: sha1_file(): Filename cannot be empty in %s on line %d
bool(false)
-- Hexadecimal Output for Empty file as Argument -- -- Hexadecimal Output for Empty file as Argument --
string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709" string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"

View File

@ -12,9 +12,18 @@ tidy_repair_string($s, $l, $l);
tidy_repair_string($s, $s, $s); tidy_repair_string($s, $s, $s);
tidy_repair_string($l, $l, $l); tidy_repair_string($l, $l, $l);
tidy_repair_file($s, $l, $l, $l); try {
tidy_repair_file($s, $s, $s, $s); tidy_repair_file($s, $l, $l, $l);
tidy_repair_file($l, $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"; 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 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_string(): Could not set encoding '1' in %s on line %d
Path cannot be empty
Warning: tidy_repair_file(): Filename cannot be empty in %s on line %d Path cannot be empty
Warning: tidy_repair_file(): Filename cannot be empty in %s on line %d
Done Done

View File

@ -25,24 +25,17 @@ $variation = array(
foreach ( $variation as $var ) { 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-- --EXPECT--
Warning: gzfile(): Filename cannot be empty in %s on line %d Path cannot be empty
bool(false) Path cannot be empty
Path cannot be empty
Warning: gzfile(): Filename cannot be empty in %s on line %d Path cannot be empty
bool(false) Path cannot be empty
Path cannot be empty
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)

View File

@ -25,24 +25,17 @@ $variation = array(
foreach ( $variation as $var ) { 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-- --EXPECT--
Warning: readgzfile(): Filename cannot be empty in %s on line %d Path cannot be empty
bool(false) Path cannot be empty
Path cannot be empty
Warning: readgzfile(): Filename cannot be empty in %s on line %d Path cannot be empty
bool(false) Path cannot be empty
Path cannot be empty
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)

View File

@ -2066,7 +2066,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mod
} }
if (!path || !*path) { if (!path || !*path) {
php_error_docref(NULL, E_WARNING, "Filename cannot be empty"); zend_value_error("Path cannot be empty");
return NULL; return NULL;
} }