Skip to content

Commit c5fe1a1

Browse files
committed
Improve error messages of ext/hash
Closes GH-5275
1 parent d5871e2 commit c5fe1a1

File tree

8 files changed

+55
-68
lines changed

8 files changed

+55
-68
lines changed

ext/hash/hash.c

Lines changed: 21 additions & 34 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_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
259-
RETURN_THROWS();
260-
}
261-
else if (!ops->is_crypto) {
262-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
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_throw_error(NULL, "Invalid 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));
@@ -361,18 +357,18 @@ PHP_FUNCTION(hash_init)
361357

362358
ops = php_hash_fetch_ops(algo);
363359
if (!ops) {
364-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
360+
zend_argument_value_error(1, "must be a valid hashing algorithm");
365361
RETURN_THROWS();
366362
}
367363

368364
if (options & PHP_HASH_HMAC) {
369365
if (!ops->is_crypto) {
370-
zend_throw_error(NULL, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
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)) {
374370
/* Note: a zero length key is no key at all */
375-
zend_throw_error(NULL, "HMAC requested without a key");
371+
zend_argument_value_error(3, "cannot be empty when HMAC is requested");
376372
RETURN_THROWS();
377373
}
378374
}
@@ -649,28 +645,23 @@ PHP_FUNCTION(hash_hkdf)
649645
}
650646

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

662653
if (ZSTR_LEN(ikm) == 0) {
663-
zend_throw_error(NULL, "Input keying material cannot be empty");
654+
zend_argument_value_error(2, "cannot be empty");
664655
RETURN_THROWS();
665656
}
666657

667658
if (length < 0) {
668-
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
659+
zend_argument_value_error(3, "must be greater than or equal to 0");
669660
RETURN_THROWS();
670661
} else if (length == 0) {
671662
length = ops->digest_size;
672663
} else if (length > (zend_long) (ops->digest_size * 255)) {
673-
zend_throw_error(NULL, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
664+
zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
674665
RETURN_THROWS();
675666
}
676667

@@ -749,27 +740,23 @@ PHP_FUNCTION(hash_pbkdf2)
749740
}
750741

751742
ops = php_hash_fetch_ops(algo);
752-
if (!ops) {
753-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
743+
if (!ops || !ops->is_crypto) {
744+
zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
754745
RETURN_THROWS();
755746
}
756-
else if (!ops->is_crypto) {
757-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
747+
748+
if (salt_len > INT_MAX - 4) {
749+
zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
758750
RETURN_THROWS();
759751
}
760752

761753
if (iterations <= 0) {
762-
zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
754+
zend_argument_value_error(4, "must be greater than 0");
763755
RETURN_THROWS();
764756
}
765757

766758
if (length < 0) {
767-
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
768-
RETURN_THROWS();
769-
}
770-
771-
if (salt_len > INT_MAX - 4) {
772-
zend_throw_error(NULL, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
759+
zend_argument_value_error(5, "must be greater than or equal to 0");
773760
RETURN_THROWS();
774761
}
775762

@@ -875,12 +862,12 @@ PHP_FUNCTION(hash_equals)
875862

876863
/* We only allow comparing string to prevent unexpected results. */
877864
if (Z_TYPE_P(known_zval) != IS_STRING) {
878-
zend_type_error("Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
865+
zend_argument_type_error(1, "must be of type string, %s given", zend_zval_type_name(known_zval));
879866
RETURN_THROWS();
880867
}
881868

882869
if (Z_TYPE_P(user_zval) != IS_STRING) {
883-
zend_type_error("Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
870+
zend_argument_type_error(2, "must be of type string, %s given", zend_zval_type_name(user_zval));
884871
RETURN_THROWS();
885872
}
886873

ext/hash/tests/hash_equals.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ bool(false)
3939
bool(false)
4040
bool(false)
4141
bool(true)
42-
[TypeError] Expected known_string to be a string, int given
43-
[TypeError] Expected user_string to be a string, int given
44-
[TypeError] Expected known_string to be a string, int given
45-
[TypeError] Expected known_string to be a string, null given
46-
[TypeError] Expected known_string to be a string, null given
47-
[TypeError] Expected known_string to be a string, null given
42+
[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, int given
43+
[TypeError] hash_equals(): Argument #2 ($user_string) must be of type string, int given
44+
[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, int given
45+
[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
46+
[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
47+
[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given

ext/hash/tests/hash_hkdf_edges.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ Length < digestSize: 98b16391063ece
3131
Length % digestSize != 0: 98b16391063ecee006a3ca8ee5776b1e5f
3232
Algo name case-sensitivity: true
3333
Non-crypto algo name case-sensitivity:
34-
[Error] Non-cryptographic hashing algorithm: jOaAt
34+
[Error] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm

ext/hash/tests/hash_hkdf_error.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ trycatch_dump(
5353
*** Testing hash_hkdf(): error conditions ***
5454

5555
-- Testing hash_hkdf() function with invalid hash algorithm --
56-
[Error] Unknown hashing algorithm: foo
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 --
59-
[Error] Non-cryptographic hashing algorithm: adler32
60-
[Error] Non-cryptographic hashing algorithm: crc32
61-
[Error] Non-cryptographic hashing algorithm: crc32b
62-
[Error] Non-cryptographic hashing algorithm: fnv132
63-
[Error] Non-cryptographic hashing algorithm: fnv1a32
64-
[Error] Non-cryptographic hashing algorithm: fnv164
65-
[Error] Non-cryptographic hashing algorithm: fnv1a64
66-
[Error] Non-cryptographic hashing algorithm: joaat
59+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
60+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
61+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
62+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
63+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
64+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
65+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
66+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
6767

6868
-- Testing hash_hkdf() function with invalid parameters --
69-
[Error] Input keying material cannot be empty
70-
[Error] Length must be greater than or equal to 0: -1
71-
[Error] Length must be less than or equal to 5100: 5101
69+
[ValueError] hash_hkdf(): Argument #2 ($ikm) cannot be empty
70+
[ValueError] hash_hkdf(): Argument #3 ($length) must be greater than or equal to 0
71+
[ValueError] hash_hkdf(): Argument #3 ($length) must be less than or equal to 5100

ext/hash/tests/hash_hmac_error.phpt

Lines changed: 2 additions & 2 deletions
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-
Unknown hashing algorithm: foo
36+
hash_hmac(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
3737

3838
-- Testing hash_hmac() function with non-cryptographic hash algorithm --
39-
Non-cryptographic hashing algorithm: crc32
39+
hash_hmac(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm

ext/hash/tests/hash_hmac_file_error.phpt

Lines changed: 4 additions & 4 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,10 +43,10 @@ catch (\Error $e) {
4343
*** Testing hash() : error conditions ***
4444

4545
-- Testing hash_hmac_file() function with invalid hash algorithm --
46-
Unknown hashing algorithm: foo
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 --
49-
Non-cryptographic hashing algorithm: crc32
49+
hash_hmac_file(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
5050

5151
-- Testing hash_hmac_file() function with bad path --
52-
Invalid path
52+
hash_hmac_file(): Argument #2 ($data) must be a valid path

ext/hash/tests/hash_init_error.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ catch (\Error $e) {
4141
*** Testing hash_init(): error conditions ***
4242

4343
-- Testing hash_init() function with unknown algorithms --
44-
Unknown hashing algorithm: dummy
44+
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-
HMAC requested with a non-cryptographic hashing algorithm: crc32
47+
hash_init(): Argument #1 ($algo) must be a cryptographic hashing algorithm if HMAC is requested
4848

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

ext/hash/tests/hash_pbkdf2_error.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ catch (\Error $e) {
5757
*** Testing hash_pbkdf2() : error conditions ***
5858

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

6262
-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --
63-
Non-cryptographic hashing algorithm: crc32
63+
hash_pbkdf2(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
6464

6565
-- Testing hash_pbkdf2() function with invalid iterations --
66-
Iterations must be a positive integer: 0
67-
Iterations must be a positive integer: -1
66+
hash_pbkdf2(): Argument #4 ($iterations) must be greater than 0
67+
hash_pbkdf2(): Argument #4 ($iterations) must be greater than 0
6868

6969
-- Testing hash_pbkdf2() function with invalid length --
70-
Length must be greater than or equal to 0: -1
70+
hash_pbkdf2(): Argument #5 ($length) must be greater than or equal to 0

0 commit comments

Comments
 (0)