Skip to content

Commit 900afb6

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix handling of invalid iterator in zend_weakmap_iterator_get_current_key()
2 parents dd45d85 + 1d94fb8 commit 900afb6

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PHP NEWS
1212
- Core:
1313
. Fixed bug GH-16168 (php 8.1 and earlier crash immediately when compiled
1414
with Xcode 16 clang on macOS 15). (nielsdos)
15+
. Fixed bug GH-16371 (Assertion failure in Zend/zend_weakrefs.c:646). (Arnaud)
1516

1617
- Curl:
1718
. Fixed bug GH-16302 (CurlMultiHandle holds a reference to CurlHandle if

Zend/tests/gh16371.phpt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
GH-16371: Assertion failure in zend_weakmap_iterator_get_current_key() for invalid iterator
3+
--FILE--
4+
<?php
5+
6+
$map = new WeakMap();
7+
$it = $map->getIterator();
8+
9+
print "# Empty WeakMap\n";
10+
11+
var_dump($it->key());
12+
var_dump($it->current());
13+
var_dump($it->valid());
14+
15+
$map = new WeakMap();
16+
$obj = new stdClass;
17+
$map[$obj] = 0;
18+
19+
print "# Valid iterator\n";
20+
21+
$it = $map->getIterator();
22+
var_dump($it->key());
23+
var_dump($it->current());
24+
var_dump($it->valid());
25+
26+
print "# End of iterator\n";
27+
28+
$it->next();
29+
var_dump($it->key());
30+
var_dump($it->current());
31+
var_dump($it->valid());
32+
33+
?>
34+
--EXPECTF--
35+
# Empty WeakMap
36+
NULL
37+
NULL
38+
bool(false)
39+
# Valid iterator
40+
object(stdClass)#%d (0) {
41+
}
42+
int(0)
43+
bool(true)
44+
# End of iterator
45+
NULL
46+
NULL
47+
bool(false)

Zend/zend_weakrefs.c

+4
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter
622622
zend_string *string_key;
623623
zend_ulong num_key;
624624
int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
625+
if (key_type == HASH_KEY_NON_EXISTENT) {
626+
ZVAL_NULL(key);
627+
return;
628+
}
625629
if (key_type != HASH_KEY_IS_LONG) {
626630
ZEND_ASSERT(0 && "Must have integer key");
627631
}

0 commit comments

Comments
 (0)