Skip to content

Commit 05d03fb

Browse files
committed
Promote warnings to Error in FileInfo extension
1 parent 9a71d47 commit 05d03fb

File tree

4 files changed

+73
-41
lines changed

4 files changed

+73
-41
lines changed

ext/fileinfo/fileinfo.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
376376
break;
377377

378378
default:
379-
php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments");
380-
RETURN_FALSE;
379+
zend_argument_type_error(2, "must be of type string|resource, %s given", zend_zval_type_name(what));
380+
RETURN_THROWS();
381381
}
382382

383383
magic = magic_open(MAGIC_MIME_TYPE);
@@ -439,14 +439,12 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
439439
php_stream_wrapper *wrap;
440440
php_stream_statbuf ssb;
441441

442-
if (buffer == NULL || !*buffer) {
443-
php_error_docref(NULL, E_WARNING, "Empty filename or path");
444-
RETVAL_FALSE;
442+
if (buffer == NULL || buffer_len == 0) {
443+
zend_argument_value_error(1, "cannot be empty");
445444
goto clean;
446445
}
447446
if (CHECK_NULL_PATH(buffer, buffer_len)) {
448-
php_error_docref(NULL, E_WARNING, "Invalid path");
449-
RETVAL_FALSE;
447+
zend_argument_type_error(1, "must not contain null bytes");
450448
goto clean;
451449
}
452450

@@ -487,6 +485,9 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
487485

488486
default:
489487
php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments");
488+
// TODO Not sure I understand this code flow completely
489+
//zend_argument_type_error(2, "must be of type string|resource, %s given", zend_zval_type_name(group));
490+
//goto clean;
490491
}
491492

492493
common:

ext/fileinfo/tests/finfo_file_001.phpt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,29 @@ finfo_file(): Testing file names
66
<?php
77

88
$fp = finfo_open();
9-
var_dump(finfo_file($fp, "\0"));
10-
var_dump(finfo_file($fp, ''));
11-
var_dump(finfo_file($fp, NULL));
9+
try {
10+
var_dump(finfo_file($fp, "\0"));
11+
} catch (\TypeError $e) {
12+
echo $e->getMessage() . \PHP_EOL;
13+
}
14+
try {
15+
var_dump(finfo_file($fp, ''));
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . \PHP_EOL;
18+
}
19+
try {
20+
var_dump(finfo_file($fp, NULL));
21+
} catch (\ValueError $e) {
22+
echo $e->getMessage() . \PHP_EOL;
23+
}
1224
var_dump(finfo_file($fp, '.'));
1325
var_dump(finfo_file($fp, '&'));
1426

1527
?>
1628
--EXPECTF--
17-
Warning: finfo_file(): Empty filename or path in %s on line %d
18-
bool(false)
19-
20-
Warning: finfo_file(): Empty filename or path in %s on line %d
21-
bool(false)
22-
23-
Warning: finfo_file(): Empty filename or path in %s on line %d
24-
bool(false)
29+
finfo_file(): Argument #1 ($finfo) must not contain null bytes
30+
finfo_file(): Argument #1 ($finfo) cannot be empty
31+
finfo_file(): Argument #1 ($finfo) cannot be empty
2532
string(9) "directory"
2633

2734
Warning: finfo_file(&): Failed to open stream: No such file or directory in %s on line %d

ext/fileinfo/tests/finfo_file_basic.phpt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ echo "*** Testing finfo_file() : basic functionality ***\n";
1313
var_dump( finfo_file( $finfo, __FILE__) );
1414
var_dump( finfo_file( $finfo, __FILE__, FILEINFO_CONTINUE ) );
1515
var_dump( finfo_file( $finfo, $magicFile ) );
16-
var_dump( finfo_file( $finfo, $magicFile.chr(0).$magicFile) );
16+
17+
try {
18+
var_dump( finfo_file( $finfo, $magicFile.chr(0).$magicFile) );
19+
} catch (\TypeError $e) {
20+
echo $e->getMessage() . \PHP_EOL;
21+
}
1722

1823
?>
19-
--EXPECTF--
24+
--EXPECT--
2025
*** Testing finfo_file() : basic functionality ***
2126
string(28) "text/x-php; charset=us-ascii"
22-
string(%d) "PHP script, ASCII text%A"
27+
string(22) "PHP script, ASCII text"
2328
string(32) "text/plain; charset=unknown-8bit"
24-
25-
Warning: finfo_file(): Invalid path in %s%efinfo_file_basic.php on line %d
26-
bool(false)
29+
finfo_file(): Argument #1 ($finfo) must not contain null bytes

ext/fileinfo/tests/mime_content_type_001.phpt

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,47 @@ mime_content_type(): Testing wrong parameters
55
--FILE--
66
<?php
77

8-
mime_content_type(1);
9-
mime_content_type(NULL);
10-
mime_content_type(new stdclass);
11-
mime_content_type(array());
8+
try {
9+
mime_content_type(1);
10+
} catch (\TypeError $e) {
11+
echo $e->getMessage() . \PHP_EOL;
12+
}
13+
try {
14+
mime_content_type(NULL);
15+
} catch (\TypeError $e) {
16+
echo $e->getMessage() . \PHP_EOL;
17+
}
18+
try {
19+
mime_content_type(new stdclass);
20+
} catch (\TypeError $e) {
21+
echo $e->getMessage() . \PHP_EOL;
22+
}
23+
try {
24+
mime_content_type(array());
25+
} catch (\TypeError $e) {
26+
echo $e->getMessage() . \PHP_EOL;
27+
}
28+
1229
mime_content_type('foo/inexistent');
13-
mime_content_type('');
14-
mime_content_type("\0");
30+
31+
try {
32+
mime_content_type('');
33+
} catch (\ValueError $e) {
34+
echo $e->getMessage() . \PHP_EOL;
35+
}
36+
try {
37+
mime_content_type("\0");
38+
} catch (\TypeError $e) {
39+
echo $e->getMessage() . \PHP_EOL;
40+
}
1541

1642
?>
1743
--EXPECTF--
18-
Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
19-
20-
Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
21-
22-
Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
23-
24-
Warning: mime_content_type(): Can only process string or stream arguments in %s on line %d
44+
mime_content_type(): Argument #2 must be of type string|resource, int given
45+
mime_content_type(): Argument #2 must be of type string|resource, null given
46+
mime_content_type(): Argument #2 must be of type string|resource, stdClass given
47+
mime_content_type(): Argument #2 must be of type string|resource, array given
2548

2649
Warning: mime_content_type(foo/inexistent): Failed to open stream: No such file or directory in %s on line %d
27-
28-
Warning: mime_content_type(): Empty filename or path in %s on line %d
29-
30-
Warning: mime_content_type(): Empty filename or path in %s on line %d
50+
mime_content_type(): Argument #1 ($filename) cannot be empty
51+
mime_content_type(): Argument #1 ($filename) must not contain null bytes

0 commit comments

Comments
 (0)