From d0ecc83ab5e250c4ddaf4b415c55e5b21957967e Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 17 Nov 2021 15:47:29 +0100 Subject: [PATCH 1/2] Assert hash is known when we claim it is --- Zend/zend_hash.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Zend/zend_hash.c b/Zend/zend_hash.c index 8ee292106a6..5f878b2154b 100644 --- a/Zend/zend_hash.c +++ b/Zend/zend_hash.c @@ -646,6 +646,7 @@ static zend_always_inline Bucket *zend_hash_find_bucket(const HashTable *ht, zen if (known_hash) { h = ZSTR_H(key); + ZEND_ASSERT(h != 0 && "Hash must be known"); } else { h = zend_string_hash_val(key); } From 6641e3b8f42795bd5955f59112c459435131d5ba Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 17 Nov 2021 15:53:25 +0100 Subject: [PATCH 2/2] Fix bug #81630: Don't claim known hash in getTraitAliases() We don't intern this string, and this code is not particularly performance critical in the first place, so just drop the the assumption. --- NEWS | 2 ++ ext/reflection/php_reflection.c | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/NEWS b/NEWS index 65d7d24ca40..7b98ac4c13c 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,8 @@ PHP NEWS - Reflection: . Fixed bug #81611 (ArgumentCountError when getting default value from ReflectionParameter with new). (Cameron Porter) + . Fixed bug #81630 (PHP 8.1: ReflectionClass->getTraitAliases() crashes with + Internal error). (Nikita) - XML: . Fixed bug #79971 (special character is breaking the path in xml function). diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 11024b9aee8..2134d9aa72f 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -5116,18 +5116,15 @@ ZEND_METHOD(ReflectionClass, getTraitAliases) if (!class_name) { uint32_t j = 0; - zval *zv; - zend_class_entry *trait; zend_string *lcname = zend_string_tolower(cur_ref->method_name); for (j = 0; j < ce->num_traits; j++) { - zv = zend_hash_find_known_hash(CG(class_table), ce->trait_names[j].lc_name); - if (zv) { - trait = Z_CE_P(zv); - if (zend_hash_exists(&trait->function_table, lcname)) { - class_name = trait->name; - break; - } + zend_class_entry *trait = + zend_hash_find_ptr(CG(class_table), ce->trait_names[j].lc_name); + ZEND_ASSERT(trait && "Trait must exist"); + if (zend_hash_exists(&trait->function_table, lcname)) { + class_name = trait->name; + break; } } zend_string_release_ex(lcname, 0);