Skip to content

Commit da1f323

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix use-after-free in RecursiveIteratorIterator on reentry
2 parents fe49107 + ec5a4f4 commit da1f323

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎ext/spl/spl_iterators.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
266266
zend_clear_exception();
267267
}
268268
}
269+
iterator = object->iterators[object->level].iterator;
269270
ZEND_FALLTHROUGH;
270271
case RS_START:
271272
if (iterator->funcs->valid(iterator) == FAILURE) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
RecursiveIteratorIterator: rewind() re-entered from an inner next() must not use-after-free
3+
--FILE--
4+
<?php
5+
class Reenter implements RecursiveIterator {
6+
public $data; public $pos = 0; public $rii; public $depth;
7+
public static bool $fired = false;
8+
function __construct(array $d, $depth = 0) { $this->data = $d; $this->depth = $depth; }
9+
function current(): mixed { return $this->data[$this->pos] ?? null; }
10+
function key(): mixed { return $this->pos; }
11+
function next(): void {
12+
$this->pos++;
13+
if ($this->rii && $this->depth === 1 && $this->pos === 1 && !self::$fired) {
14+
self::$fired = true;
15+
$this->rii->rewind();
16+
}
17+
}
18+
function rewind(): void { $this->pos = 0; }
19+
function valid(): bool { return $this->pos < count($this->data); }
20+
function hasChildren(): bool { return is_array($this->current()); }
21+
function getChildren(): RecursiveIterator {
22+
$c = new Reenter($this->current(), $this->depth + 1);
23+
$c->rii = $this->rii;
24+
return $c;
25+
}
26+
}
27+
$root = new Reenter([[10, 11], [20, 21]]);
28+
$rii = new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST);
29+
$root->rii = $rii;
30+
$seen = [];
31+
foreach ($rii as $v) {
32+
if (is_array($v)) { $v = '[' . implode(',', $v) . ']'; }
33+
$seen[] = $v;
34+
if (count($seen) > 20) { $seen[] = '...'; break; }
35+
}
36+
echo implode(' ', $seen), "\n";
37+
echo "done\n";
38+
?>
39+
--EXPECT--
40+
[10,11] 10 [10,11] 10 11 [20,21] 20 21
41+
done

0 commit comments

Comments
 (0)