|
| 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