Skip to content

ValueError for empty path in stream code #5902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions ext/standard/tests/file/file_get_contents_variation8-win32.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

?>
Expand All @@ -56,30 +56,24 @@ 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: " " --

Warning: file_get_contents( ): Failed to open stream: Permission denied in %s on line %d
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 --

Expand Down
28 changes: 11 additions & 17 deletions ext/standard/tests/file/file_get_contents_variation8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down
44 changes: 19 additions & 25 deletions ext/standard/tests/file/file_put_contents_variation8-win32.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,49 @@ $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: " " --

Warning: file_put_contents( ): Failed to open stream: Permission denied in %s on line %d
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 --

Expand Down
49 changes: 21 additions & 28 deletions ext/standard/tests/file/file_put_contents_variation8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 ***
30 changes: 18 additions & 12 deletions ext/standard/tests/file/readfile_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 --

Expand Down
20 changes: 8 additions & 12 deletions ext/standard/tests/file/readfile_variation10-win32.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***
Expand All @@ -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 --

Expand Down
Binary file modified ext/standard/tests/file/readfile_variation10.phpt
Binary file not shown.
Binary file modified ext/standard/tests/strings/md5_file.phpt
Binary file not shown.
20 changes: 12 additions & 8 deletions ext/standard/tests/strings/sha1_file.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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") );
Expand All @@ -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") );
Expand All @@ -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 --

Expand All @@ -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"
Expand Down
Loading