Skip to content

Commit 5898e8e

Browse files
committed
Promote warning to exception in file_get_contents() function
1 parent ad4d663 commit 5898e8e

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

ext/standard/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ PHP_FUNCTION(file_get_contents)
543543
ZEND_PARSE_PARAMETERS_END();
544544

545545
if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
546-
php_error_docref(NULL, E_WARNING, "length must be greater than or equal to zero");
547-
RETURN_FALSE;
546+
zend_value_error("Length must be greater than or equal to zero");
547+
return;
548548
}
549549

550550
context = php_stream_context_from_zval(zcontext, 0);

ext/standard/tests/file/file_get_contents_error.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ print( file_get_contents("/no/such/file/or/dir") );
2020
create_files($file_path, 1, "text", 0755, 100, "w", "file", 1, "byte");
2121
$file_handle = fopen($file_path."/file_put_contents_error.tmp", "w");
2222

23-
echo "\n-- Testing for invalid negative maxlen values --";
24-
var_dump( file_get_contents($file_path."/file1.tmp", FALSE, $file_handle, 0, -5) );
23+
echo "\n-- Testing for invalid negative maxlen values --\n";
24+
try {
25+
file_get_contents($file_path."/file1.tmp", FALSE, $file_handle, 0, -5);
26+
} catch (ValueError $exception) {
27+
echo $exception->getMessage() . "\n";
28+
}
2529

2630
delete_files($file_path, 1);
2731
fclose($file_handle);
@@ -47,7 +51,6 @@ if(file_exists($file_path."/file_put_contents1.tmp")) {
4751
Warning: file_get_contents(/no/such/file/or/dir): failed to open stream: No such file or directory in %s on line %d
4852

4953
-- Testing for invalid negative maxlen values --
50-
Warning: file_get_contents(): length must be greater than or equal to zero in %s on line %d
51-
bool(false)
54+
Length must be greater than or equal to zero
5255

5356
*** Done ***

ext/standard/tests/file/file_get_contents_error002.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ file_get_contents() test using negative parameter for length (last parameter)
77
display_errors=false
88
--FILE--
99
<?php
10-
var_dump(file_get_contents("http://checkip.dyndns.com",null,null,0,-1));
10+
try {
11+
file_get_contents("http://checkip.dyndns.com",null,null,0,-1);
12+
} catch (ValueError $exception) {
13+
echo $exception->getMessage() . "\n";
14+
}
1115
?>
1216
--EXPECT--
13-
bool(false)
17+
Length must be greater than or equal to zero

ext/standard/tests/file/file_get_contents_file_put_contents_error.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ print( file_get_contents("/no/such/file/or/dir") );
2020

2121
$file_handle = fopen($file_path."/file_put_contents.tmp", "w");
2222

23-
echo "\n-- Testing for invalid negative maxlen values --";
23+
echo "\n-- Testing for invalid negative maxlen values --\n";
2424
file_put_contents($file_path."/file_put_contents1.tmp", "Garbage data in the file");
25-
var_dump( file_get_contents($file_path."/file_put_contents1.tmp", FALSE, NULL, 0, -5) );
25+
try {
26+
file_get_contents($file_path."/file_put_contents1.tmp", FALSE, NULL, 0, -5);
27+
} catch (ValueError $exception) {
28+
echo $exception->getMessage() . "\n";
29+
}
2630

2731
fclose($file_handle);
2832

@@ -43,7 +47,6 @@ unlink($file_path."/file_put_contents1.tmp");
4347
Warning: file_get_contents(/no/such/file/or/dir): failed to open stream: No such file or directory in %s on line %d
4448

4549
-- Testing for invalid negative maxlen values --
46-
Warning: file_get_contents(): length must be greater than or equal to zero in %s on line %d
47-
bool(false)
50+
Length must be greater than or equal to zero
4851

4952
*** Done ***

0 commit comments

Comments
 (0)