php-src icon indicating copy to clipboard operation
php-src copied to clipboard

OPcache optimizer (pass 4): dim fetch on by-ref function result passed as argument yields NULL in 8.6.0RC7

Open pict0r opened this issue 2 weeks ago • 0 comments

With OPcache enabled, an array element of a by-reference function's return value, passed directly as a function argument, arrives as null (with the warning "Trying to access array offset on array"). It works without OPcache, with opcache.optimization_level=0x7FFEBFF7 (pass 4 disabled), and on PHP 8.5.4.

The following code:

<?php
declare(strict_types=1);
function &registry(): array { static $r = ['back' => '']; return $r; }
function set(string $a): void { $r = &registry(); $r['back'] = $a; }
function direct(): string { return json_encode(menu(registry()['back'])); }
function menu(string $w = ''): array { return [$w]; }
set('/x');
echo direct(), "\n";

Run with:

php -d opcache.enable_cli=1 -d opcache.file_update_protection=0 test.php

Resulted in this output:

Warning: Trying to access array offset on array in test.php on line 5

Fatal error: Uncaught TypeError: menu(): Argument #1 ($w) must be of type string, null given, called in test.php on line 5

But I expected this output instead:

["\/x"]

Also affected: static and instance methods returning by reference (consume(K::refStatic()['k']), consume($o->refMethod()['k'])), regardless of whether the callee is declared before or after the call. Not affected: by-value functions, assigning the element to a variable first, or registry()['k'] ?? null.

pict0r avatar Nov 13 '26 19:11 pict0r