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: 2021-09-27 16:54 UTC
From: bryanp at healthendeavors dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.6.31 OS: Windows 2008 server
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

Pull requests:

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
 [2021-09-27 16:54 UTC] cmb@php.net
-Status: Open +Status: Closed -Package: Math related +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2021-09-27 16:54 UTC] cmb@php.net
These (and other) float to int conversions will be deprecated as
of PHP 8.1.0[1], so this ticket can be closed.

[1] <https://wiki.php.net/rfc/implicit-float-int-deprecate>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 25 15:01:32 2024 UTC