Bug #66780 [NEW]: weird sorting behaviour due to overflow in custom comparison function

From: Date: Wed, 26 Feb 2014 10:34:15 +0000
Subject: Bug #66780 [NEW]: weird sorting behaviour due to overflow in custom comparison function
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184441@lists.php.net to get a copy of this message
From:             eric dot deruiter at amplixs dot com
Operating system: 
PHP version:      5.5.9
Package:          Arrays related
Bug Type:         Bug
Bug description:weird sorting behaviour due to overflow in custom comparison function

Description:
------------
It seems that a user defined comparison function should return an 32
integer, if you return anything bigger / smaller this will cause weird
sorting behaviour due to integer overflows.

If you write custom comparison functions it is common practise to
subtract 2 numeric values to determine which one is larger. And as
numeric values can be 64 bit in PHP, I would expect this to work
correctly.

Test script:
---------------
$items =
[1073741824,1,18014398509481984,8589934592,256,16777216,4,1024,17592186044416,32768,2199023255552,1125899906842624,34359738368,137438953472,1152921504606846976,4611686018427387904,8796093022208,140737488355328,2147483648,4398046511104,36028797018963968,9007199254740992,131072,17179869184,72057594037927936,512,68719476736,144115188075855872,288230376151711744,2048,281474976710656,9.2233720368548E+18,16,128,549755813888,536870912,8388608,70368744177664,2251799813685248,262144,4294967296,134217728,32,524288,65536,1099511627776,4503599627370496,2,2305843009213693952,35184372088832,67108864,8,33554432,2097152,562949953421312,4194304,4096,576460752303423488,274877906944,268435456,16384,8192,64,1048576];

usort($items, function($a, $b) 
  { 
    return $a - $b; 
  });

print_r($items);

usort($items, function($a, $b) 
  {
    $res = $a - $b; 
    if ($res < 0) 
      return -1; 
    else if ($res > 0) 
      return 1; 
    else 
      return 0; 
  });
  
print_r($items);

Expected result:
----------------
I would expect both comparison functions to have the exact same
behaviour, resulting in a ascending sorted array.


Actual result:
--------------
The first array is not sorted as expected: the tail of the array looks
like this:

    [47] => 288230376151711744
    [48] => 576460752303423488
    [49] => 1152921504606846976
    [50] => 2305843009213693952
    [51] => 4611686018427387904
    [52] => 9.2233720368548E+18
    [53] => 1
    [54] => 2
    [55] => 4
    [56] => 8
    [57] => 16
    [58] => 32
    [59] => 128
    [60] => 256
    [61] => 512
    [62] => 1024
    [63] => 2048



-- 
Edit bug report at https://bugs.php.net/bug.php?id=66780&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=66780&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=66780&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=66780&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=66780&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=66780&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=66780&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=66780&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=66780&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=66780&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=66780&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=66780&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=66780&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=66780&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66780&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=66780&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=66780&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=66780&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66780&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=66780&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=66780&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#184441) next »