]> granicus.if.org Git - php/commitdiff
Errorfy hash_pbkdf2
authorMark <mrandall@digitellinc.com>
Mon, 26 Aug 2019 22:31:28 +0000 (23:31 +0100)
committerJoe Watkins <krakjoe@php.net>
Thu, 29 Aug 2019 14:11:21 +0000 (16:11 +0200)
ext/hash/hash.c
ext/hash/tests/hash_pbkdf2_error.phpt

index 5ec065573fa7a0cf3046965c7743cc3ca412eab2..8a9485cb70f10edf2976b46784f26ffac3decc49 100644 (file)
@@ -739,27 +739,27 @@ PHP_FUNCTION(hash_pbkdf2)
 
        ops = php_hash_fetch_ops(algo, algo_len);
        if (!ops) {
-               php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
-               RETURN_FALSE;
+               zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
+               return;
        }
        else if (!ops->is_crypto) {
-               php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
-               RETURN_FALSE;
+               zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
+               return;
        }
 
        if (iterations <= 0) {
-               php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
-               RETURN_FALSE;
+               zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
+               return;
        }
 
        if (length < 0) {
-               php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
-               RETURN_FALSE;
+               zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
+               return;
        }
 
        if (salt_len > INT_MAX - 4) {
-               php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
-               RETURN_FALSE;
+               zend_throw_error(NULL, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
+               return;
        }
 
        context = emalloc(ops->context_size);
index a7fd08649bb43b9615a6120001a45063ef4d54bc..4cb5ede00b1a7fa01a194363e0463acbc5bfb1a9 100644 (file)
@@ -13,43 +13,60 @@ $password = 'password';
 $salt = 'salt';
 
 echo "\n-- Testing hash_pbkdf2() function with invalid hash algorithm --\n";
-var_dump(hash_pbkdf2('foo', $password, $salt, 1));
+try { 
+    var_dump(hash_pbkdf2('foo', $password, $salt, 1));
+}
+catch (\Error $e) {
+    echo $e->getMessage() . "\n";
+}
+
 
 echo "\n-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --\n";
-var_dump(hash_pbkdf2('crc32', $password, $salt, 1));
+try { 
+    var_dump(hash_pbkdf2('crc32', $password, $salt, 1));
+}
+catch (\Error $e) {
+    echo $e->getMessage() . "\n";
+}
 
 echo "\n-- Testing hash_pbkdf2() function with invalid iterations --\n";
-var_dump(hash_pbkdf2('md5', $password, $salt, 0));
-var_dump(hash_pbkdf2('md5', $password, $salt, -1));
+try { 
+    var_dump(hash_pbkdf2('md5', $password, $salt, 0));
+}
+catch (\Error $e) {
+    echo $e->getMessage() . "\n";
+}
+
+try { 
+    var_dump(hash_pbkdf2('md5', $password, $salt, -1));
+}
+catch (\Error $e) {
+    echo $e->getMessage() . "\n";
+}
 
 echo "\n-- Testing hash_pbkdf2() function with invalid length --\n";
-var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1));
+try { 
+    var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1));
+}
+catch (\Error $e) {
+    echo $e->getMessage() . "\n";
+}
 
 ?>
 ===Done===
---EXPECTF--
+--EXPECT--
 *** Testing hash_pbkdf2() : error conditions ***
 
 -- Testing hash_pbkdf2() function with invalid hash algorithm --
-
-Warning: hash_pbkdf2(): Unknown hashing algorithm: foo in %s on line %d
-bool(false)
+Unknown hashing algorithm: foo
 
 -- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --
-
-Warning: hash_pbkdf2(): Non-cryptographic hashing algorithm: crc32 in %s on line %d
-bool(false)
+Non-cryptographic hashing algorithm: crc32
 
 -- Testing hash_pbkdf2() function with invalid iterations --
-
-Warning: hash_pbkdf2(): Iterations must be a positive integer: 0 in %s on line %d
-bool(false)
-
-Warning: hash_pbkdf2(): Iterations must be a positive integer: -1 in %s on line %d
-bool(false)
+Iterations must be a positive integer: 0
+Iterations must be a positive integer: -1
 
 -- Testing hash_pbkdf2() function with invalid length --
-
-Warning: hash_pbkdf2(): Length must be greater than or equal to 0: -1 in %s on line %d
-bool(false)
+Length must be greater than or equal to 0: -1
 ===Done===