Skip to content

Commit 1f86339

Browse files
marandallkrakjoe
authored andcommitted
Warnings to Errors hash_equals
1 parent e18bac9 commit 1f86339

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

ext/hash/hash.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,13 @@ PHP_FUNCTION(hash_equals)
864864

865865
/* We only allow comparing string to prevent unexpected results. */
866866
if (Z_TYPE_P(known_zval) != IS_STRING) {
867-
php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
868-
RETURN_FALSE;
867+
zend_type_error("Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
868+
return;
869869
}
870870

871871
if (Z_TYPE_P(user_zval) != IS_STRING) {
872-
php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
873-
RETURN_FALSE;
872+
zend_type_error("Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
873+
return;
874874
}
875875

876876
if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {

ext/hash/tests/hash_equals.phpt

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,46 @@
22
Hash: hash_equals() test
33
--FILE--
44
<?php
5-
var_dump(hash_equals("same", "same"));
6-
var_dump(hash_equals("not1same", "not2same"));
7-
var_dump(hash_equals("short", "longer"));
8-
var_dump(hash_equals("longer", "short"));
9-
var_dump(hash_equals("", "notempty"));
10-
var_dump(hash_equals("notempty", ""));
11-
var_dump(hash_equals("", ""));
12-
var_dump(hash_equals(123, "NaN"));
13-
var_dump(hash_equals("NaN", 123));
14-
var_dump(hash_equals(123, 123));
15-
var_dump(hash_equals(null, ""));
16-
var_dump(hash_equals(null, 123));
17-
var_dump(hash_equals(null, null));
5+
6+
function trycatch_dump(...$tests) {
7+
foreach ($tests as $test) {
8+
try {
9+
var_dump($test());
10+
}
11+
catch (\Error $e) {
12+
echo '[' . get_class($e) . '] ' . $e->getMessage() . "\n";
13+
}
14+
}
15+
}
16+
17+
trycatch_dump(
18+
fn() => hash_equals("same", "same"),
19+
fn() => hash_equals("not1same", "not2same"),
20+
fn() => hash_equals("short", "longer"),
21+
fn() => hash_equals("longer", "short"),
22+
fn() => hash_equals("", "notempty"),
23+
fn() => hash_equals("notempty", ""),
24+
fn() => hash_equals("", ""),
25+
fn() => hash_equals(123, "NaN"),
26+
fn() => hash_equals("NaN", 123),
27+
fn() => hash_equals(123, 123),
28+
fn() => hash_equals(null, ""),
29+
fn() => hash_equals(null, 123),
30+
fn() => hash_equals(null, null),
31+
);
32+
1833
?>
19-
--EXPECTF--
34+
--EXPECT--
2035
bool(true)
2136
bool(false)
2237
bool(false)
2338
bool(false)
2439
bool(false)
2540
bool(false)
2641
bool(true)
27-
28-
Warning: hash_equals(): Expected known_string to be a string, int given in %s on line %d
29-
bool(false)
30-
31-
Warning: hash_equals(): Expected user_string to be a string, int given in %s on line %d
32-
bool(false)
33-
34-
Warning: hash_equals(): Expected known_string to be a string, int given in %s on line %d
35-
bool(false)
36-
37-
Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d
38-
bool(false)
39-
40-
Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d
41-
bool(false)
42-
43-
Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d
44-
bool(false)
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

0 commit comments

Comments
 (0)