[php-src] Issue #12430: One ArrayIterator in two foreach leads to endless loop

From: Date: Thu, 12 Oct 2023 20:48:35 +0000
Subject: [php-src] Issue #12430: One ArrayIterator in two foreach leads to endless loop
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-245556@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/12430 Author: tombrain ### Description The following code: ```php <?php class MyArrayElement { public int $nr; public function __construct(int $n) { $this->nr = $n; } } class MyArrayElementArray extends ArrayIterator { public function __construct(MyArrayElement ...$e) { parent::__construct($e); } public function current(): MyArrayElement { return parent::current(); } public function offsetGet($offset): MyArrayElement { return parent::offsetGet($offset); } } $arr = new MyArrayElementArray(); $arr[] = new MyArrayElement(1); $arr[] = new MyArrayElement(2); $arr[] = new MyArrayElement(3); // Testcode foreach($arr as $e) // first foreach { echo $e->nr; foreach($arr as $t) // second foreach { if ($t->nr == 2) { break; } } } ``` Resulted in this output: second foreach manipulate position in first foreach But I expected this output instead: Position of first foreach cannot be manipulated from outside ### PHP Version PHP 8.1.12 ### Operating System _No response_

« previous php.bugs (#245556) next »