Bug #76864 [NEW]: Returned reference is removed if executing inside a namespace

From: Date: Tue, 11 Sep 2018 19:24:11 +0000
Subject: Bug #76864 [NEW]: Returned reference is removed if executing inside a namespace
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216991@lists.php.net to get a copy of this message
From: tshumbeo at mailhouse dot biz Operating system: Linux and Windows PHP version: 7.2.9 Package: Scripting Engine problem Bug Type: Bug Bug description:Returned reference is removed if executing inside a namespace 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 bug report at https://bugs.php.net/bug.php?id=76864&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76864&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76864&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76864&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=76864&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=76864&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=76864&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=76864&r=needscript Try newer version: https://bugs.php.net/fix.php?id=76864&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=76864&r=support Expected behavior: https://bugs.php.net/fix.php?id=76864&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=76864&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=76864&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=76864&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76864&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=76864&r=dst IIS Stability: https://bugs.php.net/fix.php?id=76864&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=76864&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=76864&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=76864&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=76864&r=mysqlcfg

« previous php.bugs (#216991) next »