Skip to content

Commit bb28d61

Browse files
committed
Improve error messages of ext/hash
1 parent 0fe8319 commit bb28d61

File tree

8 files changed

+54
-54
lines changed

8 files changed

+54
-54
lines changed

ext/hash/hash.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,17 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
255255

256256
ops = php_hash_fetch_ops(algo);
257257
if (!ops) {
258-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
258+
zend_argument_value_error(1, "must be a valid hashing algorithm");
259259
RETURN_THROWS();
260260
}
261261
else if (!ops->is_crypto) {
262-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
262+
zend_argument_value_error(1, "must be a cryptographic hashing algorithm");
263263
RETURN_THROWS();
264264
}
265265

266266
if (isfilename) {
267267
if (CHECK_NULL_PATH(data, data_len)) {
268-
zend_throw_error(NULL, "Invalid path");
268+
zend_argument_value_error(2, "must be a valid path");
269269
RETURN_THROWS();
270270
}
271271
stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
@@ -361,18 +361,18 @@ PHP_FUNCTION(hash_init)
361361

362362
ops = php_hash_fetch_ops(algo);
363363
if (!ops) {
364-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
364+
zend_argument_value_error(1, "must be a valid hashing algorithm");
365365
RETURN_THROWS();
366366
}
367367

368368
if (options & PHP_HASH_HMAC) {
369369
if (!ops->is_crypto) {
370-
zend_throw_error(NULL, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
370+
zend_argument_value_error(2, "must not request HMAC with a non-cryptographic hashing algorithm");
371371
RETURN_THROWS();
372372
}
373373
if (!key || (ZSTR_LEN(key) == 0)) {
374374
/* Note: a zero length key is no key at all */
375-
zend_throw_error(NULL, "HMAC requested without a key");
375+
zend_argument_value_error(3, "cannot be empty when HMAC is requested");
376376
RETURN_THROWS();
377377
}
378378
}
@@ -650,27 +650,27 @@ PHP_FUNCTION(hash_hkdf)
650650

651651
ops = php_hash_fetch_ops(algo);
652652
if (!ops) {
653-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
653+
zend_argument_value_error(1, "must be a valid hashing algorithm");
654654
RETURN_THROWS();
655655
}
656656

657657
if (!ops->is_crypto) {
658-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
658+
zend_argument_value_error(1, "must be a cryptographic hashing algorithm");
659659
RETURN_THROWS();
660660
}
661661

662662
if (ZSTR_LEN(ikm) == 0) {
663-
zend_throw_error(NULL, "Input keying material cannot be empty");
663+
zend_argument_value_error(2, "cannot be empty");
664664
RETURN_THROWS();
665665
}
666666

667667
if (length < 0) {
668-
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
668+
zend_argument_value_error(3, "must be greater than or equal to 0");
669669
RETURN_THROWS();
670670
} else if (length == 0) {
671671
length = ops->digest_size;
672672
} 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);
673+
zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
674674
RETURN_THROWS();
675675
}
676676

@@ -750,26 +750,26 @@ PHP_FUNCTION(hash_pbkdf2)
750750

751751
ops = php_hash_fetch_ops(algo);
752752
if (!ops) {
753-
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
753+
zend_argument_value_error(1, "must be a valid hashing algorithm");
754754
RETURN_THROWS();
755755
}
756756
else if (!ops->is_crypto) {
757-
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
757+
zend_argument_value_error(1, "must be a cryptographic hashing algorithm");
758758
RETURN_THROWS();
759759
}
760760

761-
if (iterations <= 0) {
762-
zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
761+
if (salt_len > INT_MAX - 4) {
762+
zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
763763
RETURN_THROWS();
764764
}
765765

766-
if (length < 0) {
767-
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
766+
if (iterations <= 0) {
767+
zend_argument_value_error(4, "must be greater than 0");
768768
RETURN_THROWS();
769769
}
770770

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);
771+
if (length < 0) {
772+
zend_argument_value_error(5, "must be greater than or equal to 0");
773773
RETURN_THROWS();
774774
}
775775

@@ -875,12 +875,12 @@ PHP_FUNCTION(hash_equals)
875875

876876
/* We only allow comparing string to prevent unexpected results. */
877877
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));
878+
zend_argument_type_error(1, "must be of type string, %s given", zend_zval_type_name(known_zval));
879879
RETURN_THROWS();
880880
}
881881

882882
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));
883+
zend_argument_type_error(2, "must be of type string, %s given", zend_zval_type_name(user_zval));
884884
RETURN_THROWS();
885885
}
886886

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 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 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 cryptographic hashing algorithm
60+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm
61+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm
62+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm
63+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm
64+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm
65+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a cryptographic hashing algorithm
66+
[ValueError] hash_hkdf(): Argument #1 ($algo) must be a 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 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 cryptographic hashing algorithm

ext/hash/tests/hash_hmac_file_error.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 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 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 #2 ($options) must not request HMAC with a non-cryptographic hashing algorithm
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 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 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)