Skip to content

Commit 0787247

Browse files
committed
Merge branch 'PHP-8.2'
2 parents 7304b56 + 53558ff commit 0787247

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Zend/tests/gh11222.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GH-112222: foreach by-ref may jump over keys during a rehash
3+
--FILE--
4+
<?php
5+
6+
// Not packed
7+
$a = ["k" => 0, 1 => 1, 2, 3, 4, 5, 6];
8+
foreach ($a as $k => &$v) {
9+
if ($k == 1) {
10+
// force that it'll be rehashed by adding enough holes
11+
unset($a[4], $a[5]);
12+
// actually make the array larger than 8 elements to trigger rehash
13+
$a[] = 8; $a[] = 9; $a[] = 10;
14+
15+
}
16+
// observe the iteration jumping from key 1 to key 6, skipping keys 2 and 3
17+
echo "$k => $v\n";
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
k => 0
23+
1 => 1
24+
2 => 2
25+
3 => 3
26+
6 => 6
27+
7 => 8
28+
8 => 9
29+
9 => 10

Zend/zend_hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht)
13121312
}
13131313
}
13141314
} else {
1315-
uint32_t iter_pos = zend_hash_iterators_lower_pos(ht, 0);
1315+
uint32_t iter_pos = zend_hash_iterators_lower_pos(ht, i + 1);
13161316

13171317
while (++i < ht->nNumUsed) {
13181318
p++;

0 commit comments

Comments
 (0)