Skip to content

Commit 28fc06b

Browse files
authored
zend_hash: Assert that the interned parameter is not a lie (php#19843)
* zend_hash: Assert that the `interned` parameter is not a lie While investigating php#19842 I was wondering why non-interned string didn't cause troubles, until I realized it was the value instead of the key. Nevertheless it appears useful to check that the key is actually interned as claimed by the caller to prevent hard-to-find bugs. * zend_hash: Rename `interned` parameter name to `key_guaranteed_interned`
1 parent 1644af2 commit 28fc06b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Zend/zend_hash.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,14 +1621,15 @@ static zend_always_inline bool zend_array_is_list(const zend_array *array)
16211621
}
16221622

16231623

1624-
static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, bool interned)
1624+
static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, bool key_guaranteed_interned)
16251625
{
16261626
uint32_t idx = ht->nNumUsed++;
16271627
uint32_t nIndex;
16281628
Bucket *p = ht->arData + idx;
16291629

16301630
ZVAL_COPY_VALUE(&p->val, zv);
1631-
if (!interned && !ZSTR_IS_INTERNED(key)) {
1631+
ZEND_ASSERT(!key_guaranteed_interned || ZSTR_IS_INTERNED(key));
1632+
if (!key_guaranteed_interned && !ZSTR_IS_INTERNED(key)) {
16321633
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
16331634
zend_string_addref(key);
16341635
zend_string_hash_val(key);
@@ -1647,14 +1648,15 @@ static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *ke
16471648
return _zend_hash_append_ex(ht, key, zv, 0);
16481649
}
16491650

1650-
static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, bool interned)
1651+
static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, bool key_guaranteed_interned)
16511652
{
16521653
uint32_t idx = ht->nNumUsed++;
16531654
uint32_t nIndex;
16541655
Bucket *p = ht->arData + idx;
16551656

16561657
ZVAL_PTR(&p->val, ptr);
1657-
if (!interned && !ZSTR_IS_INTERNED(key)) {
1658+
ZEND_ASSERT(!key_guaranteed_interned || ZSTR_IS_INTERNED(key));
1659+
if (!key_guaranteed_interned && !ZSTR_IS_INTERNED(key)) {
16581660
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
16591661
zend_string_addref(key);
16601662
zend_string_hash_val(key);

0 commit comments

Comments
 (0)