From: Mark Date: Mon, 26 Aug 2019 22:31:28 +0000 (+0100) Subject: Errorfy hash_pbkdf2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e18bac96b70d37ccfdead11b7c465a75401c58bb;p=php Errorfy hash_pbkdf2 --- diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 5ec065573f..8a9485cb70 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -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); diff --git a/ext/hash/tests/hash_pbkdf2_error.phpt b/ext/hash/tests/hash_pbkdf2_error.phpt index a7fd08649b..4cb5ede00b 100644 --- a/ext/hash/tests/hash_pbkdf2_error.phpt +++ b/ext/hash/tests/hash_pbkdf2_error.phpt @@ -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===