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

From: Date: Tue, 12 Jan 2016 15:37:38 +0000
Subject: Bug #71334 [Opn]: Cannot access array keys while uksort()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198609@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 Updated by: nikic@php.net Reported by: alex dot schneider at sevenval dot com Summary: Cannot access array keys while uksort() Status: Open Type: Bug Package: Arrays related Operating System: Linux Mint 17.3 PHP Version: 7.0.2 -Assigned To: +Assigned To: laruence Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#198609) next »