Bug #50688 [Com]: Using exceptions inside usort() callback function causes a warning

From: Date: Fri, 27 May 2016 21:01:26 +0000
Subject: Bug #50688 [Com]: Using exceptions inside usort() callback function causes a warning
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201302@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=50688&edit=1

 ID:                 50688
 Comment by:         albert at mediatribe dot net
 Reported by:        jcampbell at remindermedia dot com
 Summary:            Using exceptions inside usort() callback function
                     causes a warning
 Status:             Assigned
 Type:               Bug
 Package:            Arrays related
 Operating System:   Fedora Core 12
 PHP Version:        5.*, 6
 Assigned To:        stas
 Block user comment: N
 Private report:     N

 New Comment:

A notice within the compare function will also trigger this notice. For example:

function test($a, $b) {
  return strnatcmp($a['name'], $b['name']);
}
$e = array(
  array(
    'not-name' => 'a',
  ),
  array(
    'name' => '',
  ),
);
uasort($e, 'test');

(this is tested on PHP 5.3)


Previous Comments:
------------------------------------------------------------------------
[2016-01-03 14:46:18] white06tiger+PHPbug at gmail dot com

Related To: Bug #71238

------------------------------------------------------------------------
[2015-07-18 14:17:52] nikic@php.net

This has been fixed in PHP 7.

------------------------------------------------------------------------
[2013-08-19 19:04:24] m at rtin dot so

Sorry the use () isn't relevant, I forgot to remove it when simplifying my test 
case

------------------------------------------------------------------------
[2013-08-19 18:23:17] m at rtin dot so

I ran into a similar issue, i'm sure it'll require the same patch as it's the 
backtrace causing the problem but worth noting it doesn't require an exception to 
trigger this, just a backtrace.

$ cat usort.php
<?php
 
set_error_handler(function($errno, $errstr) {
  $bt = debug_backtrace();
  var_dump($errstr);
});
 
$arr = [1, 2];
usort($arr, function($a, $b) use ($arr) {
  trigger_error('test');
  return $a > $b;
});
 
$ php usort.php
string(4) "test"
string(59) "usort(): Array was modified by the user comparison function"

------------------------------------------------------------------------
[2013-07-09 07:21:19] jakub dot lopuszanski at nasza-klasa dot pl

I'd like to add, that you do not have to throw an exception to get this warning.
Mere creating it, also triggers the warning, as in:

<?php
function comp($a,$b){
  @new Exception("dupa");
}
$a =array(1,2,3);
  usort($a, 'comp');
var_dump($a);
?>

PHP Warning:  usort(): Array was modified by the user comparison function in 
/home/jlopuszanski/test.php on line 6

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=50688


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


Thread (20 messages)

« previous php.bugs (#201302) next »