Skip to content

Commit 4eb0731

Browse files
committed
Stale array iterator pointer
Fixes GH-19613
1 parent 96c0bc5 commit 4eb0731

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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)