Warnings to Errors hash_equals

This commit is contained in:
Mark 2019-08-26 23:44:20 +01:00 committed by Joe Watkins
parent e18bac96b7
commit 1f863399d5
No known key found for this signature in database
GPG Key ID: F9BA0ADA31CBD89E
2 changed files with 39 additions and 36 deletions

View File

@ -864,13 +864,13 @@ PHP_FUNCTION(hash_equals)
/* We only allow comparing string to prevent unexpected results. */
if (Z_TYPE_P(known_zval) != IS_STRING) {
php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
RETURN_FALSE;
zend_type_error("Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
return;
}
if (Z_TYPE_P(user_zval) != IS_STRING) {
php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
RETURN_FALSE;
zend_type_error("Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
return;
}
if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {

View File

@ -2,21 +2,36 @@
Hash: hash_equals() test
--FILE--
<?php
var_dump(hash_equals("same", "same"));
var_dump(hash_equals("not1same", "not2same"));
var_dump(hash_equals("short", "longer"));
var_dump(hash_equals("longer", "short"));
var_dump(hash_equals("", "notempty"));
var_dump(hash_equals("notempty", ""));
var_dump(hash_equals("", ""));
var_dump(hash_equals(123, "NaN"));
var_dump(hash_equals("NaN", 123));
var_dump(hash_equals(123, 123));
var_dump(hash_equals(null, ""));
var_dump(hash_equals(null, 123));
var_dump(hash_equals(null, null));
function trycatch_dump(...$tests) {
foreach ($tests as $test) {
try {
var_dump($test());
}
catch (\Error $e) {
echo '[' . get_class($e) . '] ' . $e->getMessage() . "\n";
}
}
}
trycatch_dump(
fn() => hash_equals("same", "same"),
fn() => hash_equals("not1same", "not2same"),
fn() => hash_equals("short", "longer"),
fn() => hash_equals("longer", "short"),
fn() => hash_equals("", "notempty"),
fn() => hash_equals("notempty", ""),
fn() => hash_equals("", ""),
fn() => hash_equals(123, "NaN"),
fn() => hash_equals("NaN", 123),
fn() => hash_equals(123, 123),
fn() => hash_equals(null, ""),
fn() => hash_equals(null, 123),
fn() => hash_equals(null, null),
);
?>
--EXPECTF--
--EXPECT--
bool(true)
bool(false)
bool(false)
@ -24,21 +39,9 @@ bool(false)
bool(false)
bool(false)
bool(true)
Warning: hash_equals(): Expected known_string to be a string, int given in %s on line %d
bool(false)
Warning: hash_equals(): Expected user_string to be a string, int given in %s on line %d
bool(false)
Warning: hash_equals(): Expected known_string to be a string, int given in %s on line %d
bool(false)
Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d
bool(false)
Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d
bool(false)
Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d
bool(false)
[TypeError] Expected known_string to be a string, int given
[TypeError] Expected user_string to be a string, int given
[TypeError] Expected known_string to be a string, int given
[TypeError] Expected known_string to be a string, null given
[TypeError] Expected known_string to be a string, null given
[TypeError] Expected known_string to be a string, null given