Merge branch 'PHP-8.4'

* PHP-8.4:
  Fix handling of invalid iterator in zend_weakmap_iterator_get_current_key()
This commit is contained in:
Arnaud Le Blanc 2024-10-15 12:24:01 +02:00
commit 23c60d6b72
No known key found for this signature in database
GPG Key ID: 0098C05DD15ABC13
2 changed files with 51 additions and 0 deletions

47
Zend/tests/gh16371.phpt Normal file
View File

@ -0,0 +1,47 @@
--TEST--
GH-16371: Assertion failure in zend_weakmap_iterator_get_current_key() for invalid iterator
--FILE--
<?php
$map = new WeakMap();
$it = $map->getIterator();
print "# Empty WeakMap\n";
var_dump($it->key());
var_dump($it->current());
var_dump($it->valid());
$map = new WeakMap();
$obj = new stdClass;
$map[$obj] = 0;
print "# Valid iterator\n";
$it = $map->getIterator();
var_dump($it->key());
var_dump($it->current());
var_dump($it->valid());
print "# End of iterator\n";
$it->next();
var_dump($it->key());
var_dump($it->current());
var_dump($it->valid());
?>
--EXPECTF--
# Empty WeakMap
NULL
NULL
bool(false)
# Valid iterator
object(stdClass)#%d (0) {
}
int(0)
bool(true)
# End of iterator
NULL
NULL
bool(false)

View File

@ -649,6 +649,10 @@ static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter
zend_string *string_key; zend_string *string_key;
zend_ulong num_key; zend_ulong num_key;
int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos); int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
if (key_type == HASH_KEY_NON_EXISTENT) {
ZVAL_NULL(key);
return;
}
if (key_type != HASH_KEY_IS_LONG) { if (key_type != HASH_KEY_IS_LONG) {
ZEND_ASSERT(0 && "Must have integer key"); ZEND_ASSERT(0 && "Must have integer key");
} }