Skip to content

Commit c3105a1

Browse files
committed
ValueError for empty path in stream code
Closes GH-5902
1 parent f78a091 commit c3105a1

13 files changed

+139
-165
lines changed

ext/standard/tests/file/file_get_contents_variation8-win32.phpt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ $names_arr = array(
3333
);
3434

3535
foreach($names_arr as $key =>$value) {
36-
echo "\n-- Filename: $key --\n";
37-
try {
38-
var_dump(file_get_contents($value));
39-
} catch (TypeError $e) {
40-
echo $e->getMessage(), "\n";
41-
}
36+
echo "\n-- Filename: $key --\n";
37+
try {
38+
var_dump(file_get_contents($value));
39+
} catch (\TypeError|\ValueError $e) {
40+
echo get_class($e) . ': ' . $e->getMessage(), "\n";
41+
}
4242
}
4343

4444
?>
@@ -56,30 +56,24 @@ Warning: file_get_contents(1): Failed to open stream: No such file or directory
5656
bool(false)
5757

5858
-- Filename: FALSE --
59-
60-
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
61-
bool(false)
59+
ValueError: Path cannot be empty
6260

6361
-- Filename: NULL --
64-
65-
Warning: file_get_contents(): Filename cannot be empty in %sfile_get_contents_variation8-win32.php on line %d
66-
bool(false)
62+
ValueError: Path cannot be empty
6763

6864
-- Filename: "" --
69-
70-
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
71-
bool(false)
65+
ValueError: Path cannot be empty
7266

7367
-- Filename: " " --
7468

7569
Warning: file_get_contents( ): Failed to open stream: Permission denied in %s on line %d
7670
bool(false)
7771

7872
-- Filename: \0 --
79-
file_get_contents(): Argument #1 ($filename) must be a valid path, string given
73+
TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, string given
8074

8175
-- Filename: array() --
82-
file_get_contents(): Argument #1 ($filename) must be a valid path, array given
76+
TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, array given
8377

8478
-- Filename: /no/such/file/dir --
8579

ext/standard/tests/file/file_get_contents_variation8.phpt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ $names_arr = array(
3232
);
3333

3434
for( $i=0; $i<count($names_arr); $i++ ) {
35-
echo "-- Iteration $i --\n";
36-
try {
37-
var_dump(file_get_contents($names_arr[$i]));
38-
} catch (TypeError $e) {
39-
echo $e->getMessage(), "\n";
40-
}
35+
echo "-- Iteration $i --\n";
36+
try {
37+
var_dump(file_get_contents($names_arr[$i]));
38+
} catch (\TypeError|\ValueError $e) {
39+
echo get_class($e) . ': ' . $e->getMessage(), "\n";
40+
}
4141
}
4242

4343
echo "\n*** Done ***\n";
@@ -53,25 +53,19 @@ bool(false)
5353
Warning: file_get_contents(1): Failed to open stream: No such file or directory in %s on line %d
5454
bool(false)
5555
-- Iteration 2 --
56-
57-
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
58-
bool(false)
56+
ValueError: Path cannot be empty
5957
-- Iteration 3 --
60-
61-
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
62-
bool(false)
58+
ValueError: Path cannot be empty
6359
-- Iteration 4 --
64-
65-
Warning: file_get_contents(): Filename cannot be empty in %s on line %d
66-
bool(false)
60+
ValueError: Path cannot be empty
6761
-- Iteration 5 --
6862

6963
Warning: file_get_contents( ): Failed to open stream: No such file or directory in %s on line %d
7064
bool(false)
7165
-- Iteration 6 --
72-
file_get_contents(): Argument #1 ($filename) must be a valid path, string given
66+
TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, string given
7367
-- Iteration 7 --
74-
file_get_contents(): Argument #1 ($filename) must be a valid path, array given
68+
TypeError: file_get_contents(): Argument #1 ($filename) must be a valid path, array given
7569
-- Iteration 8 --
7670

7771
Warning: file_get_contents(/no/such/file/dir): Failed to open stream: No such file or directory in %s on line %d

ext/standard/tests/file/file_put_contents_variation8-win32.phpt

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,49 @@ $names_arr = array(
3131
);
3232

3333
foreach($names_arr as $key =>$value) {
34-
echo "\n-- Filename: $key --\n";
35-
try {
36-
$res = file_put_contents($value, "Some data");
37-
if ($res !== false && $res != null) {
38-
echo "$res bytes written to: $value\n";
39-
unlink($value);
40-
} else {
41-
echo "Failed to write data to: $key\n";
34+
echo "\n-- Filename: $key --\n";
35+
try {
36+
$res = file_put_contents($value, "Some data");
37+
if ($res !== false && $res != null) {
38+
echo "$res bytes written to: '$value'\n";
39+
unlink($value);
40+
} else {
41+
echo "Failed to write data to: $key\n";
42+
}
43+
} catch (\TypeError|\ValueError $e) {
44+
echo get_class($e) . ': ' . $e->getMessage(), "\n";
4245
}
43-
} catch (TypeError $e) {
44-
echo $e->getMessage(), "\n";
45-
}
46-
};
46+
}
4747

4848
?>
4949
--EXPECTF--
5050
*** Testing file_put_contents() : usage variation ***
5151

5252
-- Filename: -1 --
53-
9 bytes written to: -1
53+
9 bytes written to: '-1'
5454

5555
-- Filename: TRUE --
56-
9 bytes written to: 1
56+
9 bytes written to: '1'
5757

5858
-- Filename: FALSE --
59-
60-
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
61-
Failed to write data to: FALSE
59+
ValueError: Path cannot be empty
6260

6361
-- Filename: NULL --
64-
65-
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
66-
Failed to write data to: NULL
62+
ValueError: Path cannot be empty
6763

6864
-- Filename: "" --
69-
70-
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
71-
Failed to write data to: ""
65+
ValueError: Path cannot be empty
7266

7367
-- Filename: " " --
7468

7569
Warning: file_put_contents( ): Failed to open stream: Permission denied in %s on line %d
7670
Failed to write data to: " "
7771

7872
-- Filename: \0 --
79-
file_put_contents(): Argument #1 ($filename) must be a valid path, string given
73+
TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, string given
8074

8175
-- Filename: array() --
82-
file_put_contents(): Argument #1 ($filename) must be a valid path, array given
76+
TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, array given
8377

8478
-- Filename: /no/such/file/dir --
8579

ext/standard/tests/file/file_put_contents_variation8.phpt

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,18 @@ $names_arr = array(
3636
);
3737

3838
for( $i=0; $i<count($names_arr); $i++ ) {
39-
echo "-- Iteration $i --\n";
40-
try {
41-
$res = file_put_contents($names_arr[$i], "Some data");
42-
if ($res !== false && $res != null) {
43-
echo "$res bytes written to: $names_arr[$i]\n";
44-
unlink($names_arr[$i]);
39+
echo "-- Iteration $i --\n";
40+
try {
41+
$res = file_put_contents($names_arr[$i], "Some data");
42+
if ($res !== false && $res != null) {
43+
echo "$res bytes written to: '$names_arr[$i]'\n";
44+
unlink($names_arr[$i]);
45+
} else {
46+
echo "Failed to write data to: '$names_arr[$i]'\n";
47+
}
48+
} catch (\TypeError|\ValueError $e) {
49+
echo get_class($e) . ': ' . $e->getMessage(), "\n";
4550
}
46-
else {
47-
echo "Failed to write data to: $names_arr[$i]\n";
48-
}
49-
} catch (TypeError $e) {
50-
echo $e->getMessage(), "\n";
51-
}
5251
}
5352
rmdir($dir);
5453

@@ -57,34 +56,28 @@ echo "\n*** Done ***\n";
5756
--EXPECTF--
5857
*** Testing file_put_contents() : usage variation ***
5958
-- Iteration 0 --
60-
9 bytes written to: -1
59+
9 bytes written to: '-1'
6160
-- Iteration 1 --
62-
9 bytes written to: 1
61+
9 bytes written to: '1'
6362
-- Iteration 2 --
64-
65-
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
66-
Failed to write data to:
63+
ValueError: Path cannot be empty
6764
-- Iteration 3 --
68-
69-
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
70-
Failed to write data to:
65+
ValueError: Path cannot be empty
7166
-- Iteration 4 --
72-
73-
Warning: file_put_contents(): Filename cannot be empty in %s on line %d
74-
Failed to write data to:
67+
ValueError: Path cannot be empty
7568
-- Iteration 5 --
76-
9 bytes written to:
69+
9 bytes written to: ' '
7770
-- Iteration 6 --
78-
file_put_contents(): Argument #1 ($filename) must be a valid path, string given
71+
TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, string given
7972
-- Iteration 7 --
80-
file_put_contents(): Argument #1 ($filename) must be a valid path, array given
73+
TypeError: file_put_contents(): Argument #1 ($filename) must be a valid path, array given
8174
-- Iteration 8 --
8275

8376
Warning: file_put_contents(%sdir): Failed to open stream: %s in %s on line %d
84-
Failed to write data to: %sir
77+
Failed to write data to: '%sir'
8578
-- Iteration 9 --
8679

8780
Warning: file_put_contents(%sphp): Failed to open stream: %s in %s on line %d
88-
Failed to write data to: %sphp
81+
Failed to write data to: '%sphp'
8982

9083
*** Done ***

ext/standard/tests/file/readfile_error.phpt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ echo "*** Test readfile(): error conditions ***\n";
88

99
echo "\n-- Testing readfile() with invalid arguments --\n";
1010
// invalid arguments
11-
var_dump( readfile(NULL) ); // NULL as $filename
12-
var_dump( readfile('') ); // empty string as $filename
13-
var_dump( readfile(false) ); // boolean false as $filename
11+
try {
12+
var_dump( readfile(NULL) ); // NULL as $filename
13+
} catch (\ValueError $e) {
14+
echo $e->getMessage() . \PHP_EOL;
15+
}
16+
try {
17+
var_dump( readfile('') ); // empty string as $filename
18+
} catch (\ValueError $e) {
19+
echo $e->getMessage() . \PHP_EOL;
20+
}
21+
try {
22+
var_dump( readfile(false) ); // boolean false as $filename
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage() . \PHP_EOL;
25+
}
1426

1527
echo "\n-- Testing readfile() with non-existent file --\n";
1628
$non_existent_file = __DIR__."/non_existent_file.tmp";
@@ -22,15 +34,9 @@ echo "Done\n";
2234
*** Test readfile(): error conditions ***
2335

2436
-- Testing readfile() with invalid arguments --
25-
26-
Warning: readfile(): Filename cannot be empty in %s on line %d
27-
bool(false)
28-
29-
Warning: readfile(): Filename cannot be empty in %s on line %d
30-
bool(false)
31-
32-
Warning: readfile(): Filename cannot be empty in %s on line %d
33-
bool(false)
37+
Path cannot be empty
38+
Path cannot be empty
39+
Path cannot be empty
3440

3541
-- Testing readfile() with non-existent file --
3642

ext/standard/tests/file/readfile_variation10-win32.phpt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ foreach($names_arr as $key => $value) {
3535
echo "\n-- Filename: $key --\n";
3636
try {
3737
readfile($value);
38-
} catch (TypeError $e) {
39-
echo $e->getMessage(), "\n";
38+
} catch (\TypeError|\ValueError $e) {
39+
echo get_class($e) . ': ' . $e->getMessage(), "\n";
4040
}
41-
};
42-
41+
}
4342
?>
4443
--EXPECTF--
4544
*** Testing readfile() : variation ***
@@ -53,26 +52,23 @@ Warning: readfile(-1): Failed to open stream: No such file or directory in %s on
5352
Warning: readfile(1): Failed to open stream: No such file or directory in %s on line %d
5453

5554
-- Filename: FALSE --
56-
57-
Warning: readfile(): Filename cannot be empty in %s on line %d
55+
ValueError: Path cannot be empty
5856

5957
-- Filename: NULL --
60-
61-
Warning: readfile(): Filename cannot be empty in %s on line %d
58+
ValueError: Path cannot be empty
6259

6360
-- Filename: "" --
64-
65-
Warning: readfile(): Filename cannot be empty in %s on line %d
61+
ValueError: Path cannot be empty
6662

6763
-- Filename: " " --
6864

6965
Warning: readfile( ): Failed to open stream: Permission denied in %s on line %d
7066

7167
-- Filename: \0 --
72-
readfile(): Argument #1 ($filename) must be a valid path, string given
68+
TypeError: readfile(): Argument #1 ($filename) must be a valid path, string given
7369

7470
-- Filename: array() --
75-
readfile(): Argument #1 ($filename) must be a valid path, array given
71+
TypeError: readfile(): Argument #1 ($filename) must be a valid path, array given
7672

7773
-- Filename: /no/such/file/dir --
7874

-28 Bytes
Binary file not shown.
43 Bytes
Binary file not shown.

ext/standard/tests/strings/sha1_file.phpt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ fclose($handle2);
3232
echo "\n*** Testing for error conditions ***\n";
3333

3434
echo "\n-- No filename --\n";
35-
var_dump( sha1_file("") );
35+
try {
36+
var_dump( sha1_file("") );
37+
} catch (\ValueError $e) {
38+
echo $e->getMessage() . \PHP_EOL;
39+
}
3640

3741
echo "\n-- invalid filename --\n";
3842
var_dump( sha1_file("rewncwYcn89q") );
@@ -41,7 +45,11 @@ echo "\n-- Scalar value as filename --\n";
4145
var_dump( sha1_file(12) );
4246

4347
echo "\n-- NULL as filename --\n";
44-
var_dump( sha1_file(NULL) );
48+
try {
49+
var_dump( sha1_file(NULL) );
50+
} catch (\ValueError $e) {
51+
echo $e->getMessage() . \PHP_EOL;
52+
}
4553

4654
echo "\n-- Hexadecimal Output for Empty file as Argument --\n";
4755
var_dump( sha1_file("EmptyFileSHA1.txt") );
@@ -66,9 +74,7 @@ unlink("EmptyFileSHA1.txt");
6674
*** Testing for error conditions ***
6775

6876
-- No filename --
69-
70-
Warning: sha1_file(): Filename cannot be empty in %s on line %d
71-
bool(false)
77+
Path cannot be empty
7278

7379
-- invalid filename --
7480

@@ -81,9 +87,7 @@ Warning: sha1_file(12): Failed to open stream: No such file or directory in %s o
8187
bool(false)
8288

8389
-- NULL as filename --
84-
85-
Warning: sha1_file(): Filename cannot be empty in %s on line %d
86-
bool(false)
90+
Path cannot be empty
8791

8892
-- Hexadecimal Output for Empty file as Argument --
8993
string(40) "da39a3ee5e6b4b0d3255bfef95601890afd80709"

0 commit comments

Comments
 (0)