Skip to content

Commit f8e9e94

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Stale array iterator pointer
2 parents d73466a + e4c9482 commit f8e9e94

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PHP NEWS
77
emits a warning. (Girgias)
88
. Fixed bug GH-19637 (Incorrect Closure scope for FCC in constant
99
expression). (timwolla)
10+
. Fixed bug GH-19613 (Stale array iterator pointer). (ilutov)
1011

1112
- Date:
1213
. Fixed date_sunrise() and date_sunset() with partial-hour UTC offset.

Zend/tests/gh19613.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-19613: Invalidated array iterator pointer after array separation
3+
--FILE--
4+
<?php
5+
6+
$a = [1];
7+
$i = 0;
8+
9+
foreach ($a as &$v) {
10+
$a[0] = $a;
11+
foreach ($v as &$w) {
12+
$w = $a;
13+
14+
if ($i++ == 64) {
15+
die("===DONE===\n");
16+
}
17+
}
18+
}
19+
20+
?>
21+
--EXPECT--
22+
===DONE===

Zend/zend_hash.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,15 @@ ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval
634634
&& EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
635635
HT_DEC_ITERATORS_COUNT(iter->ht);
636636
}
637-
SEPARATE_ARRAY(array);
638-
ht = Z_ARRVAL_P(array);
637+
638+
/* Inlined SEPARATE_ARRAY() with updating of iterator when EG(ht_iterators) grows. */
639+
if (UNEXPECTED(GC_REFCOUNT(ht) > 1)) {
640+
ZVAL_ARR(array, zend_array_dup(ht));
641+
GC_TRY_DELREF(ht);
642+
iter = EG(ht_iterators) + idx;
643+
ht = Z_ARRVAL_P(array);
644+
}
645+
639646
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
640647
HT_INC_ITERATORS_COUNT(ht);
641648
}

0 commit comments

Comments
 (0)