[php-src] Issue #13178: Foreach loop with value by reference and using `unset` and set to array creates overflow memory.

From: Date: Wed, 17 Jan 2024 12:41:58 +0000
Subject: [php-src] Issue #13178: Foreach loop with value by reference and using `unset` and set to array creates overflow memory.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-246282@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/13178 Author: Tomanhez ### Description The following code: ```php $array = [ new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), new stdClass(), ]; foreach ($array as $key => &$value) { unset($array[$key]); $array[] = $value;// the same situation with $array[$key] = $value; } ``` Resulted in this output: ``` If array elements are light there is possible to get 'Process exited with code 137.' But if are higher than stdClass it kill all application. Using foreach without using $key: foreach ($array as &$value) generates: Fatal error: Allowed memory size of... - IMO this is better message for debugging what is wrong ``` But I expected this output instead: ``` Better message to be able to debug it, or possibility to use above construction :] ``` ### PHP Version 8.2 ### Operating System Ubuntu 22

« previous php.bugs (#246282) next »