Bug #78779 [Com]: Null Coalescing Operator: Trying to access array offset on value of type null

From: Date: Tue, 05 Nov 2019 11:39:36 +0000
Subject: Bug #78779 [Com]: Null Coalescing Operator: Trying to access array offset on value of type null
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223568@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78779&edit=1

 ID:                 78779
 Comment by:         bugreport2 at gmail dot com
 Reported by:        bugreport2 at gmail dot com
 Summary:            Null Coalescing Operator: Trying to access array
                     offset on value of type null
 Status:             Feedback
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        7.4Git-2019-11-04 (Git)
 Block user comment: N
 Private report:     N

 New Comment:

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] ?? '');


Previous Comments:
------------------------------------------------------------------------
[2019-11-05 11:20:40] nikic@php.net

I can't reproduce this: https://3v4l.org/9NvcY

------------------------------------------------------------------------
[2019-11-04 12:31:12] 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'



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78779&edit=1


Thread (13 messages)

« previous php.bugs (#223568) next »