php-developer-list | 2002112

[PHP-DEV] Re: #19259 [Csd->Ctl]: sort-functions don't work From: Michael Mauch (michael.mauch <email protected>)
Date: 11/16/02

nohn <email protected> wrote:
> ID: 19259
> Updated by: nohn <email protected>
> -Summary: usort() leaves array unsorted
> Reported By: bug020905 <email protected>
> -Status: Closed
> +Status: Critical
> Bug Type: Arrays related
> -Operating System: OSF1 V4.0 1229
> +Operating System: OSF1 V4.0
> -PHP Version: 4.2.2
> +PHP Version: 4.3.0 RC1
[...]
> ---- EXPECTED OUTPUT
> -- Testing arsort() --
> No second argument:
> array(8) {
> ["-2147483647"]=>
> array(2) {
> [0]=>
> string(6) "banana"
> [1]=>
> string(6) "orange"
> }
> ["test"]=>
> int(27)
> [2147483647]=>
> string(4) "test"
> [-2147483648]=>
> string(6) "monkey"
[...]
> ---- ACTUAL OUTPUT
> -- Testing arsort() --
> No second argument:
> array(8) {
> ["-2147483647"]=>
> array(2) {
> [0]=>
> string(6) "banana"
> [1]=>
> string(6) "orange"
> }
> ["test"]=>
> int(27)
> [2147483647]=>
> string(4) "test"
> [2147483648]=>
> string(6) "monkey"

These test results scared me as well, but it looks like this array test
itsself is flawed: it relies on the fact that integers automatically
wrap around to negative values at INT_MAX (=2147483647 on 32 bit
machines). Have a look at the unsorted data in data.inc:

On Linux/Intel (32 bit integers):

# sapi/cli/php -r 'include("ext/standard/tests/array/data.inc"); var_dump($data);'
array(8) {
  [0]=>
  string(3) "PHP"
  [17]=>
  string(27) "PHP: Hypertext Preprocessor"
  [5]=>
  string(4) "Test"
  ["test"]=>
  int(27)
  [2147483647]=>
  string(4) "test"
  ["-2147483647"]=>
  array(2) {
    [0]=>
    string(6) "banana"
    [1]=>
    string(6) "orange"
  }
  [-2147483648]=>
  string(6) "monkey"
  [16777216]=>
  float(-0.33333333333333)
}

On Tru64/Alpha (64 bit integers):

# sapi/cli/php -r 'include("ext/standard/tests/array/data.inc"); var_dump($data);'
Unaligned access pid=156100 <php> va=0x1400510cc pc=0x12019ab50 ra=0x12019ab44 inst=0xb4010000
Unaligned access pid=156100 <php> va=0x14005272c pc=0x12019ab50 ra=0x12019ab44 inst=0xb4010000
array(8) {
  [0]=>
  string(3) "PHP"
  [17]=>
  string(27) "PHP: Hypertext Preprocessor"
  [5]=>
  string(4) "Test"
  ["test"]=>
  int(27)
  [2147483647]=>
  string(4) "test"
  ["-2147483647"]=>
  array(2) {
    [0]=>
    string(6) "banana"
    [1]=>
    string(6) "orange"
  }
  [2147483648]=>
  string(6) "monkey"
  [16777216]=>
  float(-0.33333333333333)
}

The difference is in the key of the "monkey" element: it should be
2147483647+1, but on 32 bit machines, this number is automatically
wrapped and so it results in -2147483648. On 64 bit machines,
2147483647+1 correctly results in +2147483648, so it's impossible to get
the expected result there.

Regards...
                Michael

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php