Bug #66780 [Opn->Ver]: weird sorting behaviour due to overflow in custom comparison function

From: Date: Fri, 26 Aug 2016 18:44:26 +0000
Subject: Bug #66780 [Opn->Ver]: weird sorting behaviour due to overflow in custom comparison function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203594@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66780&edit=1 ID: 66780 Updated by: cmb@php.net Reported by: eric dot deruiter at amplixs dot com Summary: weird sorting behaviour due to overflow in custom comparison function -Status: Open +Status: Verified Type: Bug Package: Arrays related PHP Version: 5.5.9 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: This issue has been fixed as of PHP 7.0.0[1], but as it is a bug, that should probably be backported to PHP 5.6. [1] <https://3v4l.org/sWNi7> Previous Comments: ------------------------------------------------------------------------ [2014-02-26 10:34:14] eric dot deruiter at amplixs dot com 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 this bug report at https://bugs.php.net/bug.php?id=66780&edit=1

« previous php.bugs (#203594) next »