Bug #76864 [Opn->Csd]: Returned reference is removed if executing inside a namespace

From: Date: Wed, 04 Mar 2020 10:39:28 +0000
Subject: Bug #76864 [Opn->Csd]: Returned reference is removed if executing inside a namespace
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225878@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76864&edit=1 ID: 76864 Updated by: nikic@php.net Reported by: tshumbeo at mailhouse dot biz Summary: Returned reference is removed if executing inside a namespace -Status: Open +Status: Closed Type: Bug Package: Scripting Engine problem Operating System: Linux and Windows PHP Version: 7.2.9 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Closing per above comment. Previous Comments: ------------------------------------------------------------------------ [2018-09-14 13:07:55] sjon at hortensius dot net This is fixed in 7.3, see https://3v4l.org/QE9Vf since most commits don't compile I am unable to bisect this ------------------------------------------------------------------------ [2018-09-11 19:24:11] tshumbeo at mailhouse dot biz Description: ------------ Tested on PHP 5.6.37 and 7.2.9, Windows and Linux, various configurations. See the test script. When (1) is present (executing inside any namespace except global) and (2) does *not* accept $o by reference, the returned array is missing the reference that was created. As a result, subsequent call to sort() fails with this warning: Parameter 1 to sort() expected to be a reference, value given This is mind-boggling because removing the namespace (1) leaves the reference alone. Alternatively, making f() accept $o by reference (2) also removes the problem: namespace X; function f(array &$o) ... (1) | (2) | Result ----|-----|------- X | & | no bug | & | no bug | | no bug X | | bug! Yet more, if f()'s result is directly passed into call_user_func_array() bypassing a temporary variable, the reference seems to stick and the problem doesn't appear. Test script: --------------- <?php // (1) namespace X; // (2) function f(array $o) { return [&$o['v']]; } $v = ['z', 'a']; $a = ['v' => $v]; $args = f($a); call_user_func_array('sort', $args); // Additionally, if the above 2 lines are replaced by the following // line, the bug never appears. //call_user_func_array('sort', f($a)); Expected result: ---------------- Members returned by reference must be kept regardless of the namespace, $o-by-reference or use of intermediate variables. Actual result: -------------- The reference is lost when executing under a non-global namespace, not accepting $o by reference and using an intermediate variable. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76864&edit=1

« previous php.bugs (#225878) next »