Edit report at http://bugs.php.net/bug.php?id=52899&edit=1
ID: 52899
User updated by: thorn at slonik dot sk
Reported by: thorn at slonik dot sk
Summary: Incorrect array key cast (string to integer) when
key > PHP_INT_MAX
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Linux 32-bit
PHP Version: 5.2.14
Block user comment: N
New Comment:
I tried 5.3.3 but the results are same as with 5.2.14.
Previous Comments:
------------------------------------------------------------------------
[2010-09-22 09:08:58] thorn at slonik dot sk
I opened a gentoo bug:
http://bugs.gentoo.org/show_bug.cgi?id=338280
I tried my test script on same machine with windows and it works
correctly.
I'm compiling 5.3.3 now to see if it's any different.
Will post results later.
------------------------------------------------------------------------
[2010-09-21 17:41:42] [email protected]
Can you try with a vanilla version of PHP 5.2.14?
By inspection, I don't see how this can happen. Most likely it's due to
a Gentoo patch.
------------------------------------------------------------------------
[2010-09-21 15:54:45] thorn at slonik dot sk
I'm using gentoo linux (no distro binaries)
$ php -v
PHP 5.2.14-pl0-gentoo (cli) (built: Sep 17 2010 12:47:23)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
$ uname -a
Linux gloriosa 2.6.35-tuxonice-r1 #1 SMP Wed Sep 1 08:59:13 CEST 2010
i686 Intel(R) Pentium(R) 4 CPU 2.80GHz GenuineIntel GNU/Linux
------------------------------------------------------------------------
[2010-09-21 13:54:53] [email protected]
Can you give more details about your system? Are you using distro
binaries?
I can't reproduce this:
r...@router:~# php
<?php
$array['2147483648'] = 3;
var_dump($array);
var_dump(PHP_INT_MAX);
array(1) {
["2147483648"]=>
int(3)
}
int(2147483647)
r...@router:~# php -v
PHP 5.2.14 (cli) (built: Aug 18 2010 17:16:15)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
r...@router:~# uname -a
Linux router 2.4.36 #340 Sun Jul 27 20:07:55 CEST 2008 mips unknown
------------------------------------------------------------------------
[2010-09-21 11:50:52] thorn at slonik dot sk
Description:
------------
When using string as an array's key which looks like integer - the key
is converted to integer. That is ok for integers not bigger than
PHP_INT_MAX.
But when the key is: PHP_INT_MAX < key < 3000000000 (on 32-bit system)
the key is converted to incorrect integer (it overflows :)
The key should remain string and not be converted to incorrect int.
See test script, expected and actual results.
Doc:
http://www.php.net/manual/en/language.types.array.php
A key may be either an integer or a string. If a key is the standard
representation of an integer, it will be interpreted as such (i.e. "8"
will be interpreted as 8, while "08" will be interpreted as "08").
Related bugs:
#51430
#48254
#52025
Test script:
---------------
$array['2000000000'] = 1; // ok
$array['2147483647'] = 2; // ok
$array['2147483648'] = 3; // incorrect
$array['2999999999'] = 4; // incorrect
$array['3000000000'] = 5; // ok again
var_dump($array);
Expected result:
----------------
array(5) {
[2000000000]=>
int(1)
[2147483647]=>
int(2)
["2147483648"]=>
int(3)
["2999999999"]=>
int(4)
["3000000000"]=>
int(5)
}
Actual result:
--------------
array(5) {
[2000000000]=>
int(1)
[2147483647]=>
int(2)
[-2147483648]=> // int key overflow
int(3)
[-1294967297]=> // int key overflow
int(4)
["3000000000"]=>
int(5)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52899&edit=1