php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70415 Reference &$array[] appends NULL element
Submitted: 2015-09-03 03:41 UTC Modified: 2015-09-03 06:21 UTC
From: vovan-ve at yandex dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.0RC1 OS:
Private report: No CVE-ID: None
 [2015-09-03 03:41 UTC] vovan-ve at yandex dot ru
Description:
------------
Using $array[] for reading is permitted. But reference &$array[] erroneously is allowed and appends NULL element silently.

Reproduced in all PHP versions up to latest 7.0.0rc1

https://3v4l.org/0l8OE

Test script:
---------------
function bar(&$var) {}

$foo = [10, 20];
$ref = &$foo[];
bar($foo[]);
var_dump($foo);

Expected result:
----------------
Fatal error: Cannot use [] for reading in ...

Actual result:
--------------
array(4) {
  [0]=>
  int(10)
  [1]=>
  int(20)
  [2]=>
  &NULL
  [3]=>
  NULL
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-03 06:21 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-09-03 06:21 UTC] nikic@php.net
References are note pure reads. Just like you can write =& $foo[2] even if index 2 does not yet exist, you can also write $foo[]. Taking the reference on an undefined index will initialize the index to NULL.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC