Skip to content

Commit 1ddd102

Browse files
committed
Address code review comments
1 parent bb28d61 commit 1ddd102

File tree

6 files changed

+15
-28
lines changed

6 files changed

+15
-28
lines changed

ext/hash/hash.c

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
137137
}
138138
if (isfilename) {
139139
if (CHECK_NULL_PATH(data, data_len)) {
140-
zend_argument_value_error(1, "must be a valid path");
140+
zend_argument_type_error(1, "must be a valid path");
141141
RETURN_THROWS();
142142
}
143143
stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
@@ -254,18 +254,14 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
254254
}
255255

256256
ops = php_hash_fetch_ops(algo);
257-
if (!ops) {
258-
zend_argument_value_error(1, "must be a valid hashing algorithm");
259-
RETURN_THROWS();
260-
}
261-
else if (!ops->is_crypto) {
262-
zend_argument_value_error(1, "must be a cryptographic hashing algorithm");
257+
if (!ops || !ops->is_crypto) {
258+
zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
263259
RETURN_THROWS();
264260
}
265261

266262
if (isfilename) {
267263
if (CHECK_NULL_PATH(data, data_len)) {
268-
zend_argument_value_error(2, "must be a valid path");
264+
zend_argument_type_error(2, "must be a valid path");
269265
RETURN_THROWS();
270266
}
271267
stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
@@ -367,7 +363,7 @@ PHP_FUNCTION(hash_init)
367363

368364
if (options & PHP_HASH_HMAC) {
369365
if (!ops->is_crypto) {
370-
zend_argument_value_error(2, "must not request HMAC with a non-cryptographic hashing algorithm");
366+
zend_argument_value_error(1, "must be a cryptographic hashing algorithm if HMAC is requested");
371367
RETURN_THROWS();
372368
}
373369
if (!key || (ZSTR_LEN(key) == 0)) {
@@ -649,13 +645,8 @@ PHP_FUNCTION(hash_hkdf)
649645
}
650646

651647
ops = php_hash_fetch_ops(algo);
652-
if (!ops) {
653-
zend_argument_value_error(1, "must be a valid hashing algorithm");
654-
RETURN_THROWS();
655-
}
656-
657-
if (!ops->is_crypto) {
658-
zend_argument_value_error(1, "must be a cryptographic hashing algorithm");
648+
if (!ops || !ops->is_crypto) {
649+
zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
659650
RETURN_THROWS();
660651
}
661652

@@ -749,12 +740,8 @@ PHP_FUNCTION(hash_pbkdf2)
749740
}
750741

751742
ops = php_hash_fetch_ops(algo);
752-
if (!ops) {
753-
zend_argument_value_error(1, "must be a valid hashing algorithm");
754-
RETURN_THROWS();
755-
}
756-
else if (!ops->is_crypto) {
757-
zend_argument_value_error(1, "must be a cryptographic hashing algorithm");
743+
if (!ops || !ops->is_crypto) {
744+
zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
758745
RETURN_THROWS();
759746
}
760747

ext/hash/tests/hash_hkdf_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ trycatch_dump(
5353
*** Testing hash_hkdf(): error conditions ***
5454

5555
-- Testing hash_hkdf() function with invalid hash algorithm --
56-
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid hashing algorithm
56+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
5757

5858
-- Testing hash_hkdf() function with non-cryptographic hash algorithm --
5959
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm

ext/hash/tests/hash_hmac_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ catch (\Error $e) {
3333
*** Testing hash_hmac() : error conditions ***
3434

3535
-- Testing hash_hmac() function with invalid hash algorithm --
36-
hash_hmac(): Argument #1 ($algo) must be a valid hashing algorithm
36+
hash_hmac(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
3737

3838
-- Testing hash_hmac() function with non-cryptographic hash algorithm --
3939
hash_hmac(): Argument #1 ($algo) must be a cryptographic hashing algorithm

ext/hash/tests/hash_hmac_file_error.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ echo "\n-- Testing hash_hmac_file() function with bad path --\n";
3434
try {
3535
var_dump(hash_hmac_file('md5', $file.chr(0).$file, $key, TRUE));
3636
}
37-
catch (\Error $e) {
37+
catch (TypeError $e) {
3838
echo $e->getMessage() . "\n";
3939
}
4040

@@ -43,7 +43,7 @@ catch (\Error $e) {
4343
*** Testing hash() : error conditions ***
4444

4545
-- Testing hash_hmac_file() function with invalid hash algorithm --
46-
hash_hmac_file(): Argument #1 ($algo) must be a valid hashing algorithm
46+
hash_hmac_file(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
4747

4848
-- Testing hash_hmac_file() function with non-cryptographic hash algorithm --
4949
hash_hmac_file(): Argument #1 ($algo) must be a cryptographic hashing algorithm

ext/hash/tests/hash_init_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ catch (\Error $e) {
4444
hash_init(): Argument #1 ($algo) must be a valid hashing algorithm
4545

4646
-- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --
47-
hash_init(): Argument #2 ($options) must not request HMAC with a non-cryptographic hashing algorithm
47+
hash_init(): Argument #2 ($options) must be a cryptographic hashing algorithm if HMAC is requested
4848

4949
-- Testing hash_init() function with HASH_HMAC and no key --
5050
hash_init(): Argument #3 ($key) cannot be empty when HMAC is requested

ext/hash/tests/hash_pbkdf2_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ catch (\Error $e) {
5757
*** Testing hash_pbkdf2() : error conditions ***
5858

5959
-- Testing hash_pbkdf2() function with invalid hash algorithm --
60-
hash_pbkdf2(): Argument #1 ($algo) must be a valid hashing algorithm
60+
hash_pbkdf2(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
6161

6262
-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --
6363
hash_pbkdf2(): Argument #1 ($algo) must be a cryptographic hashing algorithm

0 commit comments

Comments
 (0)