Fix use-after-free in Collator::sort() with a mutating comparator#22467
Closed
iliaal wants to merge 1 commit into
Closed
Fix use-after-free in Collator::sort() with a mutating comparator#22467iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
LamentXU123
approved these changes
Jun 26, 2026
LamentXU123
left a comment
Member
There was a problem hiding this comment.
Looks Good. I think would be better to add a comment here to make sure people know why we are doing this.
devnexen
reviewed
Jun 26, 2026
Member
There was a problem hiding this comment.
nit: now that the manual zend_array_dup provides the isolation, the / in "Oa/|l" is a redundant eager copy when the array is shared by value (usort skips it for the same reason). Micro optimisation but might be worth it wdyt ?
Contributor
Author
There was a problem hiding this comment.
You're right once the dup runs first. Reordered so the conversion operates on the copy and the original array is never mutated, which lets the / drop like usort does. The two conversion-failure paths free the copy.
collator_sort() and collator_asort() sort the array in place while the comparator may run a __toString() that appends to the same array through a reference, reallocating its storage under the running sort. Sort a copy and swap it back, matching usort().
8761bca to
f494d4e
Compare
devnexen
approved these changes
Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Collator::sort() and Collator::asort() with COLLATOR_SORT_REGULAR use each object element's __toString() as the comparison key. If that __toString() appends to the array being sorted (reachable through a reference), the array's backing storage is reallocated while zend_hash_sort() still holds pointers into it, a use-after-free. usort() already avoids this by duplicating the array before sorting; Collator now does the same.