Skip to content

Commit e18bac9

Browse files
marandallkrakjoe
authored andcommitted
Errorfy hash_pbkdf2
1 parent ee104cf commit e18bac9

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

ext/hash/hash.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -739,27 +739,27 @@ PHP_FUNCTION(hash_pbkdf2)
739739

740740
ops = php_hash_fetch_ops(algo, algo_len);
741741
if (!ops) {
742-
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
743-
RETURN_FALSE;
742+
zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
743+
return;
744744
}
745745
else if (!ops->is_crypto) {
746-
php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
747-
RETURN_FALSE;
746+
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
747+
return;
748748
}
749749

750750
if (iterations <= 0) {
751-
php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
752-
RETURN_FALSE;
751+
zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
752+
return;
753753
}
754754

755755
if (length < 0) {
756-
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
757-
RETURN_FALSE;
756+
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
757+
return;
758758
}
759759

760760
if (salt_len > INT_MAX - 4) {
761-
php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
762-
RETURN_FALSE;
761+
zend_throw_error(NULL, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
762+
return;
763763
}
764764

765765
context = emalloc(ops->context_size);

ext/hash/tests/hash_pbkdf2_error.phpt

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,60 @@ $password = 'password';
1313
$salt = 'salt';
1414

1515
echo "\n-- Testing hash_pbkdf2() function with invalid hash algorithm --\n";
16-
var_dump(hash_pbkdf2('foo', $password, $salt, 1));
16+
try {
17+
var_dump(hash_pbkdf2('foo', $password, $salt, 1));
18+
}
19+
catch (\Error $e) {
20+
echo $e->getMessage() . "\n";
21+
}
22+
1723

1824
echo "\n-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --\n";
19-
var_dump(hash_pbkdf2('crc32', $password, $salt, 1));
25+
try {
26+
var_dump(hash_pbkdf2('crc32', $password, $salt, 1));
27+
}
28+
catch (\Error $e) {
29+
echo $e->getMessage() . "\n";
30+
}
2031

2132
echo "\n-- Testing hash_pbkdf2() function with invalid iterations --\n";
22-
var_dump(hash_pbkdf2('md5', $password, $salt, 0));
23-
var_dump(hash_pbkdf2('md5', $password, $salt, -1));
33+
try {
34+
var_dump(hash_pbkdf2('md5', $password, $salt, 0));
35+
}
36+
catch (\Error $e) {
37+
echo $e->getMessage() . "\n";
38+
}
39+
40+
try {
41+
var_dump(hash_pbkdf2('md5', $password, $salt, -1));
42+
}
43+
catch (\Error $e) {
44+
echo $e->getMessage() . "\n";
45+
}
2446

2547
echo "\n-- Testing hash_pbkdf2() function with invalid length --\n";
26-
var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1));
48+
try {
49+
var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1));
50+
}
51+
catch (\Error $e) {
52+
echo $e->getMessage() . "\n";
53+
}
2754

2855
?>
2956
===Done===
30-
--EXPECTF--
57+
--EXPECT--
3158
*** Testing hash_pbkdf2() : error conditions ***
3259

3360
-- Testing hash_pbkdf2() function with invalid hash algorithm --
34-
35-
Warning: hash_pbkdf2(): Unknown hashing algorithm: foo in %s on line %d
36-
bool(false)
61+
Unknown hashing algorithm: foo
3762

3863
-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --
39-
40-
Warning: hash_pbkdf2(): Non-cryptographic hashing algorithm: crc32 in %s on line %d
41-
bool(false)
64+
Non-cryptographic hashing algorithm: crc32
4265

4366
-- Testing hash_pbkdf2() function with invalid iterations --
44-
45-
Warning: hash_pbkdf2(): Iterations must be a positive integer: 0 in %s on line %d
46-
bool(false)
47-
48-
Warning: hash_pbkdf2(): Iterations must be a positive integer: -1 in %s on line %d
49-
bool(false)
67+
Iterations must be a positive integer: 0
68+
Iterations must be a positive integer: -1
5069

5170
-- Testing hash_pbkdf2() function with invalid length --
52-
53-
Warning: hash_pbkdf2(): Length must be greater than or equal to 0: -1 in %s on line %d
54-
bool(false)
71+
Length must be greater than or equal to 0: -1
5572
===Done===

0 commit comments

Comments
 (0)