xref: /PHP-8.1/ext/spl/tests/bug37457.phpt (revision b3e08881)
1--TEST--
2Bug #37457 (Crash when an exception is thrown in accept() method of FilterIterator)
3--FILE--
4<?php
5
6class Collection implements Iterator
7{
8    protected $array, $valid = false;
9
10    public function __construct(array $a)
11    {
12        echo __METHOD__ . "\n";
13        $this->array = $a;
14    }
15
16    public function current(): mixed
17    {
18        echo __METHOD__ . "\n";
19        return current($this->array);
20    }
21
22    public function key(): mixed
23    {
24        echo __METHOD__ . "\n";
25        return key($this->array);
26    }
27
28    public function next(): void
29    {
30        echo __METHOD__ . "\n";
31        $this->valid = (false !== next($this->array));
32    }
33
34    public function valid(): bool
35    {
36        echo __METHOD__ . "\n";
37        return $this->valid;
38    }
39
40    public function rewind(): void
41    {
42        echo __METHOD__ . "\n";
43        $this->valid = (false !== reset($this->array));
44    }
45}
46
47class TestFilter extends FilterIterator
48{
49    public function accept(): bool
50    {
51        echo __METHOD__ . "\n";
52        throw new Exception("Failure in Accept");
53    }
54}
55
56$test = new TestFilter(new Collection(array(0)));
57
58try
59{
60    foreach ($test as $item)
61    {
62        echo $item;
63    }
64}
65catch (Exception $e)
66{
67    var_dump($e->getMessage());
68}
69
70?>
71--EXPECT--
72Collection::__construct
73Collection::rewind
74Collection::valid
75Collection::current
76Collection::key
77TestFilter::accept
78string(17) "Failure in Accept"
79