Skip to content

Foreach loop with value by reference and using unset and set to array creates overflow memory. #13178

Closed
@Tomanhez

Description

@Tomanhez

Description

The following code:

$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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions