Skip to content

Commit e4c9482

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Stale array iterator pointer
2 parents 6f17c69 + f9ce6d8 commit e4c9482

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
@@ -9,6 +9,7 @@ PHP NEWS
99
due to signed int overflow). (ilutov)
1010
. Fixed bug GH-19544 (GC treats ZEND_WEAKREF_TAG_MAP references as WeakMap
1111
references). (Arnaud, timwolla)
12+
. Fixed bug GH-19613 (Stale array iterator pointer). (ilutov)
1213

1314
- Date:
1415
. 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
@@ -635,8 +635,15 @@ ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval
635635
&& EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
636636
HT_DEC_ITERATORS_COUNT(iter->ht);
637637
}
638-
SEPARATE_ARRAY(array);
639-
ht = Z_ARRVAL_P(array);
638+
639+
/* Inlined SEPARATE_ARRAY() with updating of iterator when EG(ht_iterators) grows. */
640+
if (UNEXPECTED(GC_REFCOUNT(ht) > 1)) {
641+
ZVAL_ARR(array, zend_array_dup(ht));
642+
GC_TRY_DELREF(ht);
643+
iter = EG(ht_iterators) + idx;
644+
ht = Z_ARRVAL_P(array);
645+
}
646+
640647
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
641648
HT_INC_ITERATORS_COUNT(ht);
642649
}

0 commit comments

Comments
 (0)