php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75053 Large floats used as array keys should not silently wrap
Submitted: 2017-08-08 22:26 UTC Modified: 2017-08-12 14:36 UTC
From: bryanp at healthendeavors dot com Assigned:
Status: Open Package: Math related
PHP Version: 5.6.31 OS: Windows 2008 server
Private report: No CVE-ID:
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bryanp at healthendeavors dot com
New email:
PHP Version: OS:

 

 [2017-08-08 22:26 UTC] bryanp at healthendeavors dot com
Description:
------------
Casting large numbers to integers as array keys leads to unexpected results that are not obvious when looking at the code. It appears the 32-bit integer limit is being reached.

Casting the key to a string or triggering an error letting the user know something went wrong would be more helpful. 

I think it's much more useful to have the wrong type than the wrong value as the key. Additionally, casting to an integer has a chance to replace an existing key.

Releated bugs: 51430 shows as duplicate of 51008 which shows a status of Closed and says fixed.

Test script:
---------------
$arr = [1410065407 => 'some value'];
$arr[9999999999] = 'another value';
var_dump($arr);


Expected result:
----------------
array (size=2)
  1410065407 => string 'some value' (length=10)
  '9999999999' => string 'another value' (length=13)

Actual result:
--------------
array (size=1)
  1410065407 => string 'another value' (length=13)

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-09 15:04 UTC] andrew dot nester dot dev at gmail dot com
Thanks for reporting the problem!

I've created pull request fixing this behaviour
 [2017-08-11 12:52 UTC] andrew dot nester dot dev at gmail dot com
I also added alternative solution here: https://github.com/php/php-src/pull/2677
 [2017-08-12 14:35 UTC] ajf@php.net
Following precedent for similar changes, I think this requires an RFC.
 [2017-08-12 14:36 UTC] ajf@php.net
-Summary: Integer array key limitations +Summary: Large floats used as array keys should not silently wrap
 [2017-08-13 18:50 UTC] andrew dot nester dot dev at gmail dot com
I am mostly sure it is, that's why I started discussion on internals to discuss both solutions. In addition I don't have enough karma to start RFC yet
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC