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

 ID:                 50688
 Comment by:         eric_haney at yahoo dot com
 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:

It took me a while to figure out that some code called from usort was throwing, 
catching, and (gracefully) handling an Exception.  Then I found this post.  
Quite frustrating.

I turned off warnings with ini_set before calling usort, then turned them on 
again after.  This is an effective workaround for now, but I'd love to clean 
that nastiness out of my code.

It is also my opinion that usort should be allowed to change the elements in 
the array.  EG: an instance variable of an object may be lazy-loaded as a 
result of a method call from within a usort callback.  Should a warning really 
be issued in that case?


Previous Comments:
------------------------------------------------------------------------
[2011-10-10 21:44:56] poehler at interworx dot com

This bug is still present as of PHP 5.3.8, we ran into it today and spent most 
of a day trying to figure out what was causing the error message "Array was 
modified by the user comparison function", when CLEARLY, NOTHING was changing 
the array at all!

The exception was not thrown/caught directly in the usort function but rather 
in a constructor of a class that was called about 3 or 4 functions deep from 
the usort, making it very difficult to track down.  

After finally figuring out the exception was somehow related, we searched 
google and found this bug report.  I'm sure we can agree that the minor act of 
catching an exception should not result in usort throwing a warning message.  
This bug is a huge timewaster :(

------------------------------------------------------------------------
[2010-10-07 23:34:54] philipwhiuk at hotmail dot com

I notice this is still affecting PHP 5.3.3 (Windows/Apache install).

Is this likely to be fixed soon - is it a question of developer time and 
priority 
or is it too difficult to fix?

It's quite irritating - I realise that the obvious solution is to avoid 
throwing 
the exception (ha-ha) but it's a useful function and exceptions are... 
inevitable.

------------------------------------------------------------------------
[2010-05-31 17:24:05] ajrattink at correct dot net

I printed a debug line from my usort callback. It called debug_backtrace() to 
print the line and sourcefile in the debuglog. And therefor triggered the 
error. Even more, it did not sort.

Maybe the phpmanual should state that usort() callbacks are not allowed to 
write loglines. I also think that usort() callbacks that DO change the array 
are perfectly legal, as long as they don't change the sort.  

Maybe your sorter code needs stackoverflow protection or whatever, but calling 
certain code 'invalid', because it causes your code to SEGV is a stupid way to 
solve a bug.

------------------------------------------------------------------------
[2010-04-01 02:12:52] s...@php.net

The reason seems to be that when making exception backtrace, 
debug_backtrace_get_args() uses SEPARATE_ZVAL_TO_MAKE_IS_REF() on arguments, 
which makes it look as if the argument was indeed modified (which usort is 
designed to protect against, since cmp callback is not supposed to modify the 
arguments)

------------------------------------------------------------------------
[2010-03-05 17:41:30] bernie at dcbl dot ca

affects gentoo builds after > 5.2.10 (5.2.11, 5.2.11-r1, and 5.2.12)

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


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

Reply via email to