php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79393 Null coalescing operator failing with SplFixedArray
Submitted: 2020-03-18 12:59 UTC Modified: 2020-03-18 15:16 UTC
From: julien dot falque at gmail dot com Assigned: cmb (profile)
Status: Closed Package: SPL related
PHP Version: 7.4.4RC1 OS: Linux
Private report: No CVE-ID: None
 [2020-03-18 12:59 UTC] julien dot falque at gmail dot com
Description:
------------
This is probably related to https://bugs.php.net/bug.php?id=74478.

Using null coalescing operation on an index that does not exist in an SplFixedArray triggers an exception (Index invalid or out of range) while doing the same with isset() works as expected.

See the following snippets:
 * with isset(): https://3v4l.org/s79uA
 * with null coalescing operator: https://3v4l.org/ucSbZ

For some reason, using a class that extends SplFixedArray and overrides method offsetExists() works: https://3v4l.org/5ohF1.

Test script:
---------------
<?php

$foo = new SplFixedArray(2);
$foo[0] = 'bar1';
$foo[1] = 'bar2';

var_dump($foo[0] ?? null);
var_dump($foo[1] ?? null);
var_dump($foo[2] ?? null);


Expected result:
----------------
string(4) "bar1"
string(4) "bar2"
NULL

Actual result:
--------------
string(4) "bar1"
string(4) "bar2"

Fatal error: Uncaught RuntimeException: Index invalid or out of range in /in/ucSbZ:9
Stack trace:
#0 {main}
  thrown in /in/ucSbZ on line 9

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-18 15:16 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2020-03-18 15:16 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #79393: Null coalescing operator failing with SplFixedArray
On GitHub:  https://github.com/php/php-src/pull/5274
Patch:      https://github.com/php/php-src/pull/5274.patch
 [2020-03-23 13:06 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=47c745555c9000706cd4de7fffc6e1c2f18d5521
Log: Fix #79393: Null coalescing operator failing with SplFixedArray
 [2020-03-23 13:06 UTC] cmb@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC