Unpredictable integer overflow with ksort
After making a fix to my PHP application on my test server, I pushed it to my production server and the fix didn't work. After tracking it down I found that ksort() was causing index keys to overflow for integers larger than the max 32-bit signed value (2147483648) on my production server only. On my test server it would only overflow for integers larger than the max signed 64-bit integer (9223372036854775808).
Both servers (as fully as I can tell) have the same software configuration
- Ubuntu 12.10
- Kernel version: Linux version 3.0.0-15-generic-pae
- CPU on production: AMD Phenom(tm) II X4 910
- CPU on test server: AMD Athlon(tm) II X4 620
To reproduce this issue
$arr = array(2147483648 => 1, 5 => 2); ksort($arr); print_r($arr);
Outputs the following on my production server:
Array ( [-2147483648] => 1  => 2 )
The exact same code outputs the following on my test server:
Array (  => 2  => 1 )
I'm trying to figure out what could be causing this. Where should I be looking to narrow down why the servers have different integer sizes. Is it possible that I may have a different version of PHP (even though both appear to be the same) that was compiled with 32bit integers?
take a look at PHP_INT_SIZE
Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
I would also suggest to increase PHP memory_limit in php.ini or with ini_set().