Skip to content

Commit 1762a87

Browse files
committed
Fix GH-8366: ArrayIterator may leak when calling __construct()
When we detach an iterator, we also have to delete it. Closes GH-8374.
1 parent 789f6b8 commit 1762a87

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ PHP NEWS
1818
. Fixed bug GH-8267 (MySQLi uses unsupported format specifier on Windows).
1919
(cmb)
2020

21+
- SPL:
22+
. Fixed bug GH-8366 (ArrayIterator may leak when calling __construct()).
23+
(cmb)
24+
2125
- Streams:
2226
. Fixed php://temp does not preserve file-position when switched to temporary
2327
file. (Bernd Holzmüller)

ext/spl/spl_array.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,10 @@ static void spl_array_set_array(zval *object, spl_array_object *intern, zval *ar
11291129

11301130
intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
11311131
intern->ar_flags |= ar_flags;
1132-
intern->ht_iter = (uint32_t)-1;
1132+
if (intern->ht_iter != (uint32_t)-1) {
1133+
zend_hash_iterator_del(intern->ht_iter);
1134+
intern->ht_iter = (uint32_t)-1;
1135+
}
11331136
}
11341137
/* }}} */
11351138

ext/spl/tests/gh8366.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Bug GH-8366 (ArrayIterator may leak when calling __construct())
3+
--FILE--
4+
<?php
5+
$it = new \ArrayIterator();
6+
foreach ($it as $elt) {}
7+
$it->__construct([]);
8+
?>
9+
--EXPECT--

0 commit comments

Comments
 (0)