Bug #71334 [Com]: Cannot access array keys while uksort()

From: Date: Wed, 13 Jan 2016 16:15:12 +0000
Subject: Bug #71334 [Com]: Cannot access array keys while uksort()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198628@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71334&edit=1

 ID:                 71334
 Comment by:         jsgoupil at gmail dot com
 Reported by:        alex dot schneider at sevenval dot com
 Summary:            Cannot access array keys while uksort()
 Status:             Assigned
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux Mint 17.3
 PHP Version:        7.0.2
 Assigned To:        laruence
 Block user comment: N
 Private report:     N

 New Comment:

I have the same problem, I am able to access the values, but the sorting is not correct.

<?php
function cmp($a, $b)
{
    $x = $GLOBALS['x'];
    
    $v1 = $x[$a];
    $v2 = $x[$b];
    if ($v1 < $v2) {
        return -1;
    } elseif ($v1 > $v2) {
        return 1;
    } else {
        // Would then sort by key, irrelevant here.
    }
}

$x = array(1 => 1, 2 => 3, 3 => 2);
uksort($x, 'cmp');
var_dump($x);

// PHP5
array(3) { [1]=> int(1) [3]=> int(2) [2]=> int(3) }

// PHP7
array(3) { [3]=> int(2) [2]=> int(3) [1]=> int(1) }


Let me know if you want me to open another bug instead.


Previous Comments:
------------------------------------------------------------------------
[2016-01-13 06:36:55] laruence@php.net

hmm, we could also adjust arHash after swap, but it must not be a simple patch..

thanks

------------------------------------------------------------------------
[2016-01-12 16:36:08] nikic@php.net

It looks like ArrayObject relies on sorting functions not to duplicate the array for the rc=1 case,
so this will need some more work :(

------------------------------------------------------------------------
[2016-01-12 15:37:35] nikic@php.net

The problem is that we're doing the sort in-place, so any access to the array during the sort
will see it in an inconsistent state. I don't see any way to solve this problem without
requiring the array to always be duplicated before sorts with user-defined callbacks.

Here's a patch to implement this: https://github.com/nikic/php-src/commit/703fed650319f9e2d78e2dc2cafb422e558c5841
It also removes the array modification check, as it's no longer relevant after this change.

@laruence: You worked on the sorting functionality, do you see a way to fix this in any other way?

------------------------------------------------------------------------
[2016-01-11 11:03:05] alex dot schneider at sevenval dot com

Description:
------------
Cannot access array keys while uksort().

Sorry for my English...

Test script:
---------------
<?php

class myClass
{
	private $a = [
		'foo-test' => [1],
		'-' => [2],
		'bar-test' => [3]
	];

	private function _mySort($x, $y)
	{
		if (!isset($this->a[$x])) {
			throw new Exception('Missing X: "' . $x . '"');
		}

		if (!isset($this->a[$y])) {
			throw new Exception('Missing Y: "' . $y . '"');
		}

		return $x < $y;
	}

	public function __construct()
	{
		uksort($this->a, [$this, '_mySort']);
	}
}

new myClass();

Expected result:
----------------
No Exceptions

Actual result:
--------------
Exception:

PHP Fatal error:  Uncaught Exception: Missing Y: "bar-test" in ...


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71334&edit=1


Thread (7 messages)

« previous php.bugs (#198628) next »