Bug #78513 [NEW]: compact('0') (any int) will generate an array with inaccessible key
| From: | tandre@php.net | Date: | Sun, 08 Sep 2019 18:51:14 +0000 |
| Subject: | Bug #78513 [NEW]: compact('0') (any int) will generate an array with inaccessible key | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-222630@lists.php.net to get a copy of this message | ||
From: tandre
Operating system: Linux/Any
PHP version: Irrelevant
Package: Arrays related
Bug Type: Bug
Bug description:compact('0') (any int) will generate an array with inaccessible key
Description:
------------
compact('0') (or any other number) will generate an array with
inaccessible keys if a variable with that value is defined.
I can't think of any reason to actually do this in real code, but I'd
also expect all functions to return arrays with accessible keys.
Note that ext/opcache/Optimizer/zend_func_info.c may need to be changed
depending on how this is fixed, to add MAY_BE_ARRAY_KEY_LONG.
F1("compact", MAY_BE_NULL | MAY_BE_ARRAY |
MAY_BE_ARRAY_KEY_STRING | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_OF_ANY),
I'd suggest limiting the fix to development php versions(php 7.4/8.0).
https://bugs.php.net/bug.php?id=61655 was a
similar issue with
inaccessible array keys, but had a different cause (casting object to
array)
Test script:
---------------
<?php
call_user_func(function () {
${0} = 'value';
$result = compact('0');
var_export($result);
var_export($result[0]);
});
Expected result:
----------------
Should emit the following output, with an integer array key (0, not '0')
that can be accessed.
array(
0 => 'value'
)'value'
Alternatively, it could throw/return an error value.
Actual result:
--------------
php test.php
array (
'0' => 'value',
)Notice: Undefined offset: 0 in /home/tyson/test.php on line 6
NULL
--
Edit bug report at https://bugs.php.net/bug.php?id=78513&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=78513&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78513&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78513&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78513&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78513&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78513&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78513&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78513&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78513&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78513&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78513&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=78513&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78513&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78513&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78513&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78513&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78513&r=mysqlcfg