php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29049 usort() does not trigger an error when callback does not exist
Submitted: 2004-07-07 15:02 UTC Modified: 2004-07-08 19:07 UTC
From: bugs dot php dot net at chsc dot dk Assigned: iliaa (profile)
Status: Closed Package: Arrays related
PHP Version: 4CVS, 5CVS OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bugs dot php dot net at chsc dot dk
New email:
PHP Version: OS:

 

 [2004-07-07 15:02 UTC] bugs dot php dot net at chsc dot dk
Description:
------------
When the second argument to usort() is not a valid callback, no error is triggered, and the return value is true.

This makes debugging tricky.

When invoked with an invalid callback, usort() for some reason rearranges the values in the array anyway.

Reproduce code:
---------------
<?php
error_reporting(E_ALL);
$array = array(1, 7, 3, 2);
var_dump(usort($array, 'not a function name'));
var_dump($array);
var_dump(usort($array, 9999));
var_dump($array);
var_dump(usort($array, array('foo', 'bar')));
var_dump($array);
?>

Expected result:
----------------
The output should be three error messages, three times "bool(false)" and three times the original array.

Actual result:
--------------
bool(true)
array(4) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(7)
  [3]=>
  int(1)
}
bool(true)
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(7)
  [2]=>
  int(3)
  [3]=>
  int(2)
}
bool(true)
array(4) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(7)
  [3]=>
  int(1)
}
 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-08 11:29 UTC] sniper@php.net
There isn't any checking done in any of the u*sort() functions for the callback being valid. (zend_is_callable() should be used?)

This might be intentional, since the check would slow these functions down a bit..?

 [2004-07-08 19:07 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC