Skip to content

Commit f9ce6d8

Browse files
committed
Stale array iterator pointer
Fixes phpGH-19613 Closes phpGH-19616
1 parent 15beb14 commit f9ce6d8

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
@@ -630,8 +630,15 @@ ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval
630630
&& EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
631631
HT_DEC_ITERATORS_COUNT(iter->ht);
632632
}
633-
SEPARATE_ARRAY(array);
634-
ht = Z_ARRVAL_P(array);
633+
634+
/* Inlined SEPARATE_ARRAY() with updating of iterator when EG(ht_iterators) grows. */
635+
if (UNEXPECTED(GC_REFCOUNT(ht) > 1)) {
636+
ZVAL_ARR(array, zend_array_dup(ht));
637+
GC_TRY_DELREF(ht);
638+
iter = EG(ht_iterators) + idx;
639+
ht = Z_ARRVAL_P(array);
640+
}
641+
635642
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
636643
HT_INC_ITERATORS_COUNT(ht);
637644
}

0 commit comments

Comments
 (0)