Skip to content

Commit e035a95

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-16054: Segmentation fault when resizing hash table iterator list while adding
2 parents 7b14134 + 4b8a12d commit e035a95

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Zend/zend_hash.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -2345,17 +2345,20 @@ static zend_always_inline bool zend_array_dup_element(const HashTable *source, H
23452345

23462346
// We need to duplicate iterators to be able to search through all copy-on-write copies to find the actually iterated HashTable and position back
23472347
static void zend_array_dup_ht_iterators(const HashTable *source, HashTable *target) {
2348-
HashTableIterator *iter = EG(ht_iterators);
2349-
const HashTableIterator *end = iter + EG(ht_iterators_used);
2348+
uint32_t iter_index = 0;
2349+
uint32_t end_index = EG(ht_iterators_used);
23502350

2351-
while (iter != end) {
2351+
while (iter_index != end_index) {
2352+
HashTableIterator *iter = &EG(ht_iterators)[iter_index];
23522353
if (iter->ht == source) {
23532354
uint32_t copy_idx = zend_hash_iterator_add(target, iter->pos);
2355+
/* Refetch iter because the memory may be reallocated. */
2356+
iter = &EG(ht_iterators)[iter_index];
23542357
HashTableIterator *copy_iter = EG(ht_iterators) + copy_idx;
23552358
copy_iter->next_copy = iter->next_copy;
23562359
iter->next_copy = copy_idx;
23572360
}
2358-
iter++;
2361+
iter_index++;
23592362
}
23602363
}
23612364

ext/spl/tests/gh16054.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-16054 (Segmentation fault when resizing hash table iterator list while adding)
3+
--FILE--
4+
<?php
5+
$multi_array = ['zero'];
6+
$multi_array[] =& $multi_array;
7+
$it = new RecursiveTreeIterator(new RecursiveArrayIterator($multi_array), 0);
8+
$counter = 0;
9+
foreach ($it as $k => $v) {
10+
if (++$counter > 200) break;
11+
}
12+
echo "ok\n";
13+
?>
14+
--EXPECT--
15+
ok

0 commit comments

Comments
 (0)