php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81633 Passing array by reference to function sets key
Submitted: 2021-11-17 23:00 UTC Modified: 2021-11-17 23:16 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ogdg at protonmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 8.0.12 OS: Fedora 35
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ogdg at protonmail dot com
New email:
PHP Version: OS:

 

 [2021-11-17 23:00 UTC] ogdg at protonmail dot com
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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-17 23:16 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-11-17 23:16 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

That is not how references are designed to work.
https://www.php.net/manual/en/language.references.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC