Skip to content

Commit d5871e2

Browse files
committed
Promote warnings to exceptions in ext/hash
1 parent a42e8e8 commit d5871e2

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

ext/hash/hash.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
132132

133133
ops = php_hash_fetch_ops(algo);
134134
if (!ops) {
135-
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
136-
RETURN_FALSE;
135+
zend_argument_value_error(1, "must be a valid hashing algorithm");
136+
RETURN_THROWS();
137137
}
138138
if (isfilename) {
139139
if (CHECK_NULL_PATH(data, data_len)) {
140-
php_error_docref(NULL, E_WARNING, "Invalid path");
141-
RETURN_FALSE;
140+
zend_argument_value_error(1, "must be a valid path");
141+
RETURN_THROWS();
142142
}
143143
stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
144144
if (!stream) {
@@ -1065,8 +1065,8 @@ PHP_FUNCTION(mhash_keygen_s2k)
10651065

10661066
bytes = (int)l_bytes;
10671067
if (bytes <= 0){
1068-
php_error_docref(NULL, E_WARNING, "The byte parameter must be greater than 0");
1069-
RETURN_FALSE;
1068+
zend_argument_value_error(4, "must be a greater than 0");
1069+
RETURN_THROWS();
10701070
}
10711071

10721072
salt_len = MIN(salt_len, SALT_SIZE);

ext/hash/tests/hash_error.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ Hash: hash() function : error conditions
1111
echo "*** Testing hash() : error conditions ***\n";
1212

1313
echo "\n-- Testing hash() function with invalid hash algorithm --\n";
14-
var_dump(hash('foo', ''));
14+
try {
15+
hash('foo', '');
16+
} catch (ValueError $exception) {
17+
echo $exception->getMessage() . "\n";
18+
}
1519

1620
?>
17-
--EXPECTF--
21+
--EXPECT--
1822
*** Testing hash() : error conditions ***
1923

2024
-- Testing hash() function with invalid hash algorithm --
21-
22-
Warning: hash(): Unknown hashing algorithm: foo in %s on line %d
23-
bool(false)
25+
hash(): Argument #1 ($algo) must be a valid hashing algorithm

ext/hash/tests/hash_file_error.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ file_put_contents( $filename, 'The quick brown fox jumped over the lazy dog.' );
1919

2020
// hash_file() error tests
2121
echo "\n-- Testing hash_file() function with an unknown algorithm --\n";
22-
var_dump( hash_file( 'foobar', $filename ) );
22+
try {
23+
hash_file('foobar', $filename);
24+
} catch (ValueError $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
2327

2428
echo "\n-- Testing hash_file() function with a non-existent file --\n";
25-
var_dump( hash_file( 'md5', 'nonexistent.txt' ) );
29+
var_dump(hash_file('md5', 'nonexistent.txt'));
2630

2731
?>
2832
--CLEAN--
@@ -36,9 +40,7 @@ unlink( $filename );
3640
*** Testing hash_file() : error conditions ***
3741

3842
-- Testing hash_file() function with an unknown algorithm --
39-
40-
Warning: hash_file(): Unknown hashing algorithm: %s in %s on line %d
41-
bool(false)
43+
hash_file(): Argument #1 ($algo) must be a valid hashing algorithm
4244

4345
-- Testing hash_file() function with a non-existent file --
4446

0 commit comments

Comments
 (0)