]> granicus.if.org Git - php/commitdiff
Promote warning to exception in file_get_contents() function
authorMáté Kocsis <kocsismate@woohoolabs.com>
Wed, 20 Nov 2019 01:29:18 +0000 (02:29 +0100)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Thu, 5 Dec 2019 07:32:29 +0000 (08:32 +0100)
ext/standard/file.c
ext/standard/tests/file/file_get_contents_error.phpt
ext/standard/tests/file/file_get_contents_error002.phpt
ext/standard/tests/file/file_get_contents_file_put_contents_error.phpt

index c7727a65b8f72b361fa0e58c089c72dc02f8dcec..58bbbaa52c86477a4044665b447a929b319df710 100644 (file)
@@ -543,8 +543,8 @@ PHP_FUNCTION(file_get_contents)
        ZEND_PARSE_PARAMETERS_END();
 
        if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
-               php_error_docref(NULL, E_WARNING, "length must be greater than or equal to zero");
-               RETURN_FALSE;
+               zend_value_error("Length must be greater than or equal to zero");
+               return;
        }
 
        context = php_stream_context_from_zval(zcontext, 0);
index 9757b3be2a24c514c90be2d609fc92ed45dc760a..8304105ccced59e9696c1a7d4844258809f5d09a 100644 (file)
@@ -20,8 +20,12 @@ print( file_get_contents("/no/such/file/or/dir") );
 create_files($file_path, 1, "text", 0755, 100, "w", "file", 1, "byte");
 $file_handle = fopen($file_path."/file_put_contents_error.tmp", "w");
 
-echo "\n-- Testing for invalid negative maxlen values --";
-var_dump( file_get_contents($file_path."/file1.tmp", FALSE, $file_handle, 0, -5) );
+echo "\n-- Testing for invalid negative maxlen values --\n";
+try {
+    file_get_contents($file_path."/file1.tmp", FALSE, $file_handle, 0, -5);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 delete_files($file_path, 1);
 fclose($file_handle);
@@ -47,7 +51,6 @@ if(file_exists($file_path."/file_put_contents1.tmp")) {
 Warning: file_get_contents(/no/such/file/or/dir): failed to open stream: No such file or directory in %s on line %d
 
 -- Testing for invalid negative maxlen values --
-Warning: file_get_contents(): length must be greater than or equal to zero in %s on line %d
-bool(false)
+Length must be greater than or equal to zero
 
 *** Done ***
index 8ca11626723fa8883bb1a005ed39ee4bcbd16a0b..4c9faf91ea171696666995309ca25fd8768c818c 100644 (file)
@@ -7,7 +7,11 @@ file_get_contents() test using negative parameter for length (last parameter)
 display_errors=false
 --FILE--
 <?php
-       var_dump(file_get_contents("http://checkip.dyndns.com",null,null,0,-1));
+try {
+    file_get_contents("http://checkip.dyndns.com",null,null,0,-1);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 ?>
 --EXPECT--
-bool(false)
+Length must be greater than or equal to zero
index a50317e8dbb31a56bed84d980f9fdfdb49e1888a..b111ff9f5da72b097186e832c164eccfb03531f0 100644 (file)
@@ -20,9 +20,13 @@ print( file_get_contents("/no/such/file/or/dir") );
 
 $file_handle = fopen($file_path."/file_put_contents.tmp", "w");
 
-echo "\n-- Testing for invalid negative maxlen values --";
+echo "\n-- Testing for invalid negative maxlen values --\n";
 file_put_contents($file_path."/file_put_contents1.tmp", "Garbage data in the file");
-var_dump( file_get_contents($file_path."/file_put_contents1.tmp", FALSE, NULL, 0, -5) );
+try {
+    file_get_contents($file_path."/file_put_contents1.tmp", FALSE, NULL, 0, -5);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 fclose($file_handle);
 
@@ -43,7 +47,6 @@ unlink($file_path."/file_put_contents1.tmp");
 Warning: file_get_contents(/no/such/file/or/dir): failed to open stream: No such file or directory in %s on line %d
 
 -- Testing for invalid negative maxlen values --
-Warning: file_get_contents(): length must be greater than or equal to zero in %s on line %d
-bool(false)
+Length must be greater than or equal to zero
 
 *** Done ***