]> granicus.if.org Git - php/commitdiff
Promote warnings to Error in FileInfo extension
authorGeorge Peter Banyard <girgias@php.net>
Thu, 6 Aug 2020 11:37:36 +0000 (13:37 +0200)
committerGeorge Peter Banyard <girgias@php.net>
Thu, 6 Aug 2020 11:37:36 +0000 (13:37 +0200)
Closes GH-5914

ext/fileinfo/fileinfo.c
ext/fileinfo/tests/finfo_file_001.phpt
ext/fileinfo/tests/finfo_file_basic.phpt
ext/fileinfo/tests/mime_content_type_001.phpt

index 014c37441aee89afdc2344d6fa80978943998ace..2538171f25bee3e1a54d7335f76a9b5db4239967 100644 (file)
@@ -376,8 +376,8 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
                                break;
 
                        default:
-                               php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments");
-                               RETURN_FALSE;
+                               zend_argument_type_error(2, "must be of type resource|string, %s given", zend_zval_type_name(what));
+                               RETURN_THROWS();
                }
 
                magic = magic_open(MAGIC_MIME_TYPE);
@@ -439,14 +439,12 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
                        php_stream_wrapper *wrap;
                        php_stream_statbuf ssb;
 
-                       if (buffer == NULL || !*buffer) {
-                               php_error_docref(NULL, E_WARNING, "Empty filename or path");
-                               RETVAL_FALSE;
+                       if (buffer == NULL || buffer_len == 0) {
+                               zend_argument_value_error(1, "cannot be empty");
                                goto clean;
                        }
                        if (CHECK_NULL_PATH(buffer, buffer_len)) {
-                               php_error_docref(NULL, E_WARNING, "Invalid path");
-                               RETVAL_FALSE;
+                               zend_argument_type_error(1, "must not contain null bytes");
                                goto clean;
                        }
 
@@ -484,9 +482,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
                        }
                        break;
                }
-
-               default:
-                       php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments");
+               EMPTY_SWITCH_DEFAULT_CASE()
        }
 
 common:
index 3d01c8e88131cc63c8f3d04260472a557ae8df25..726103e02d666f750c03b9d15637a63fb8c0574e 100644 (file)
@@ -6,22 +6,29 @@ finfo_file(): Testing file names
 <?php
 
 $fp = finfo_open();
-var_dump(finfo_file($fp, "\0"));
-var_dump(finfo_file($fp, ''));
-var_dump(finfo_file($fp, NULL));
+try {
+    var_dump(finfo_file($fp, "\0"));
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(finfo_file($fp, ''));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    var_dump(finfo_file($fp, NULL));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 var_dump(finfo_file($fp, '.'));
 var_dump(finfo_file($fp, '&'));
 
 ?>
 --EXPECTF--
-Warning: finfo_file(): Empty filename or path in %s on line %d
-bool(false)
-
-Warning: finfo_file(): Empty filename or path in %s on line %d
-bool(false)
-
-Warning: finfo_file(): Empty filename or path in %s on line %d
-bool(false)
+finfo_file(): Argument #1 ($finfo) must not contain null bytes
+finfo_file(): Argument #1 ($finfo) cannot be empty
+finfo_file(): Argument #1 ($finfo) cannot be empty
 string(9) "directory"
 
 Warning: finfo_file(&): Failed to open stream: No such file or directory in %s on line %d
index 1851058b131a8d2d022edb4caa3fad0945944fa8..816f826c1b5546f35f89713a76a4030687493b1c 100644 (file)
@@ -13,14 +13,17 @@ echo "*** Testing finfo_file() : basic functionality ***\n";
 var_dump( finfo_file( $finfo, __FILE__) );
 var_dump( finfo_file( $finfo, __FILE__, FILEINFO_CONTINUE ) );
 var_dump( finfo_file( $finfo, $magicFile ) );
-var_dump( finfo_file( $finfo, $magicFile.chr(0).$magicFile) );
+
+try {
+    var_dump( finfo_file( $finfo, $magicFile.chr(0).$magicFile) );
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
---EXPECTF--
+--EXPECT--
 *** Testing finfo_file() : basic functionality ***
 string(28) "text/x-php; charset=us-ascii"
-string(%d) "PHP script, ASCII text%A"
+string(22) "PHP script, ASCII text"
 string(32) "text/plain; charset=unknown-8bit"
-
-Warning: finfo_file(): Invalid path in %s%efinfo_file_basic.php on line %d
-bool(false)
+finfo_file(): Argument #1 ($finfo) must not contain null bytes
index 472d3e84f8125253668c905961a69935d3006a50..9e3cb603986cddb93b209b33bc7ad977b29db771 100644 (file)
@@ -5,26 +5,47 @@ mime_content_type(): Testing wrong parameters
 --FILE--
 <?php
 
-mime_content_type(1);
-mime_content_type(NULL);
-mime_content_type(new stdclass);
-mime_content_type(array());
+try {
+    mime_content_type(1);
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    mime_content_type(NULL);
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    mime_content_type(new stdclass);
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    mime_content_type(array());
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+
 mime_content_type('foo/inexistent');
-mime_content_type('');
-mime_content_type("\0");
+
+try {
+    mime_content_type('');
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+try {
+    mime_content_type("\0");
+} catch (\TypeError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
 
 ?>
 --EXPECTF--
-Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
-
-Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
-
-Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
-
-Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
+mime_content_type(): Argument #2 must be of type resource|string, int given
+mime_content_type(): Argument #2 must be of type resource|string, null given
+mime_content_type(): Argument #2 must be of type resource|string, stdClass given
+mime_content_type(): Argument #2 must be of type resource|string, array given
 
 Warning: mime_content_type(foo/inexistent): Failed to open stream: No such file or directory in %s on line %d
-
-Warning: mime_content_type(): Empty filename or path in %s on line %d
-
-Warning: mime_content_type(): Empty filename or path in %s on line %d
+mime_content_type(): Argument #1 ($filename) cannot be empty
+mime_content_type(): Argument #1 ($filename) must not contain null bytes