php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78779 Null Coalescing Operator: Trying to access array offset on value of type null
Submitted: 2019-11-04 12:31 UTC Modified: 2019-12-15 04:22 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:0 (0.0%)
From: bugreport2 at gmail dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 7.4Git-2019-11-04 (Git) OS: Linux
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: bugreport2 at gmail dot com
New email:
PHP Version: OS:

 

 [2019-11-04 12:31 UTC] bugreport2 at gmail dot com
Description:
------------
"Trying to access array offset on value of type null" is new and fully dserved but in context of "Null Coalescing Operator" it makes no sense

no matter if $arr or $arr[0] is undefined, the left side is NULL and so assign '' and you are done - probably even potential for optimization

$arr = mysqli_fetch_row($result);
$oldcontent = $arr[0] ?? '';

AUTOTEST ERROR-HANDLER: 'code: 8' 'msg: Trying to access array offset on value of type null' 'file: misc.php' 'line: 116'


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-05 11:20 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2019-11-05 11:20 UTC] nikic@php.net
I can't reproduce this: https://3v4l.org/9NvcY
 [2019-11-05 11:39 UTC] bugreport2 at gmail dot com
it was triggered by a fuzzy-testsuite on two out of millions of calls :-(

i replaced the offending code path with a construct used all over the codebase which should silence it as intended 

i find it interesting that it's even possible that this warning triggers at all when "Null Coalescing Operator" is used because there is no point to look at the array offset when the whole $arr is NULL

-  $arr = mysqli_fetch_row($db->query($sql));
-  $oldcontent = $arr[0] ?? '';
+  $oldcontent = (string)(mysqli_fetch_row($db->query($sql))[0] ?? '');
 [2019-11-17 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2019-11-17 11:41 UTC] cmb@php.net
-Status: No Feedback +Status: Open
 [2019-11-17 11:41 UTC] cmb@php.net
Opening, since feedback had been provided.
 [2019-12-04 09:31 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2019-12-04 09:31 UTC] nikic@php.net
Unless there's some working reproduction code for this, I don't think we can do anything here.
 [2019-12-07 19:01 UTC] contact at kevin-anidjar dot com
Hi, here is a way to reproduce the issue:

https://3v4l.org/mX3Pf
 [2019-12-07 19:07 UTC] bugreports3 at gmail dot com
sadly "echo strval($arr['foo']) ?? 'bar';" is a invalid reproducer and expected to thow a warning becaus ein that line strval() is feeded with $arr['foo'] and "?? 'bar'" would only be relevant wehn that returns NULL which is not possible by the nature of strval()

this is a completly different beast
$oldcontent = $arr[0] ?? '';
 [2019-12-07 19:07 UTC] requinix@php.net
@kevin-anidjar: That does not reproduce the issue. The code must use ?? immediately after a value and without that value going through a function.
 [2019-12-07 19:16 UTC] contact at kevin-anidjar dot com
@requinix@php.net: in previous PHP versions, this code did not throw an error and the returned value was instead considered as null.
 [2019-12-07 19:21 UTC] bugreports3 at gmail dot com
> in previous PHP versions, this code did not throw an error 
> and the returned value was instead considered as null

but your code is *expected* to throw an error in newer versions while "$arr[0] ?? ''" is not, no matter if $arr or $arr[0] is undefined
 [2019-12-07 19:28 UTC] contact at kevin-anidjar dot com
My bad, did not see this in the changelog:

Array-style access of non-arrays

Trying to use values of type null, bool, int, float or resource as an array (such as $null["key"]) will now generate a notice.
 [2019-12-15 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC