Bug #81633 [NEW]: Passing array by reference to function sets key
| From: | ogdg at protonmail dot com | Date: | Wed, 17 Nov 2021 23:00:24 +0000 |
| Subject: | Bug #81633 [NEW]: Passing array by reference to function sets key | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-237832@lists.php.net to get a copy of this message | ||
From: ogdg at protonmail dot com
Operating system: Fedora 35
PHP version: 8.0.12
Package: Arrays related
Bug Type: Bug
Bug description:Passing array by reference to function sets key
Description:
------------
For any user-defined function where the argument is passed by reference,
and an array is passed with reference to an undefined key as the
argument for the function, this causes the key to be set in the array.
Test script:
---------------
function test(&$a) {
return;
}
If you have an empty array:
$b = array();
And do this:
test($b[5]);
The array $b will now have a key '5' set.
Expected result:
----------------
This should not happen, but it does and it it affects PHP 7 as well,
possibly other versions. The variable/array passed as the function
argument should not be altered or set unless the function itself
deliberately makes changes. I cannot think of any good reason that a
value should be set merely by passing it as a function argument.
The reason I discovered this is that PHP 8 now throws warnings for
"undefined array key", and to mitigate this I changed several functions
to allow the arguments to be passed by reference. While this was a
notice on older versions of PHP, it should be completely removed from
the "error reporting" of PHP and/or made into a debugging feature that
is disabled by default.
Many scripts check for the existence of a key in an array and if the key
does not exist then no special message should be generated. There is no
reason that is_array() should throw an "undefined key" warning; if the
key isn't there then any checks for its existence should simply respond
logically, i.e. false or null.
I suspect that this has been the cause of many PHP applications
suffering from mysterious errors or other unexpected behaviors, so I
hope that we can fix this without cumbersome "workarounds" like using
the null coalescing operator.
--
Edit bug report at https://bugs.php.net/bug.php?id=81633&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=81633&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=81633&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=81633&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=81633&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=81633&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=81633&r=support
Expected behavior: https://bugs.php.net/fix.php?id=81633&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=81633&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=81633&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=81633&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81633&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=81633&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=81633&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=81633&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=81633&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=81633&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=81633&r=mysqlcfg