Skip to content

Promote warnings to Error in FileInfo extension #5914

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 2 commits 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
16 changes: 6 additions & 10 deletions ext/fileinfo/fileinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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:
Expand Down
29 changes: 18 additions & 11 deletions ext/fileinfo/tests/finfo_file_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions ext/fileinfo/tests/finfo_file_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
55 changes: 38 additions & 17 deletions ext/fileinfo/tests/mime_content_type_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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