-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix SORT_REGULAR transitivity for mixed types #20315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jmarble
wants to merge
7
commits into
php:master
Choose a base branch
from
jmarble:fix-sort-regular-transitivity-tls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+397
−2
Conversation
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
6e73f5a to
7f9704c
Compare
Fixes phpGH-20262: SORT_REGULAR now uses transitive comparison for consistent sorting of mixed numeric/non-numeric strings and types. Added EG(transitive_compare_mode) flag with save/restore pattern to enforce ordering: numeric-types < numeric-strings < non-numeric. This fixes sort() and array_unique() inconsistencies with mixed types, nested arrays, and objects. Comparison operators unchanged.
7f9704c to
438b127
Compare
Empty strings must sort before numbers to match PHP 8+ semantics where '' < 5 is true. Updated compare_long_to_string(), compare_double_to_string(), and zendi_smart_strcmp() to handle empty string as a special case in transitive mode.
Without initialization, transitive_compare_mode had garbage values on Windows ZTS, causing the flag to leak into normal comparisons. Initialize to false in init_executor().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Some suggested additional tests that could help:
$a = ["", "0", "00", "A"];
$b = ["A", "00", "0", ""];
sort($a, SORT_REGULAR);
sort($b, SORT_REGULAR);
var_dump($a === $b); // expect true
// and expected order: ["", "0", "00", "A"]?
$a = [" 5", "+5", "-0", "0", "A"];
sort($a, SORT_REGULAR);
// expect numeric ones before "A", retaining numeric ordering: ["-0","0","+5"," 5","A"] and stable or no (+5 vs [space]5 insertion order)?
$a = ["5e2", "500", "NAN", "INF", "-INF"];
sort($a, SORT_REGULAR);
// So: ["500","5e2", "-INF","INF","NAN"]?
$a = ["0x10", "16", "0b10000"];
sort($a, SORT_REGULAR);
// expect: ["16", "0b10000", "0x10"]?
$a = [10, "3A", 5, "10", ""];
sort($a, SORT_REGULAR);
// expect: ["", 5, 10, "10", "3A"]?
$a = ["9223372036854775807", "9223372036854775808", 9223372036854775807];
sort($a, SORT_REGULAR);
// ensure consistency across LONG_MAX vs double promotionsSuggested-by: Rob Landers <landers.robert@gmail.com>
- Add gh20262.phpt: Bug reproduction test (replaces sort_regular_transitive*.phpt) - Add sort_variation_numeric_strings.phpt: Edge case tests for numeric strings Suggested-by: Rob Landers <landers.robert@gmail.com>
On x32, 9223372036854775807 becomes float instead of int.
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.
Problem
array_unique()withSORT_REGULARwas missing duplicates, andsort()was producing inconsistent results depending on input order when comparing mixed types (numeric strings, non-numeric strings, integers, doubles).The root cause: SORT_REGULAR was using PHP's comparison operators (which are non-transitive by design) directly in sorting algorithms that require transitive comparisons to function correctly.
When comparing these types, the comparison created cycles:
"5" < "10"(numeric: 5 < 10)"10" < "3A"(lexicographic: '1' < '3')"5" > "3A"(lexicographic: '5' > '3') Creates a cycleThis violated the fundamental requirement of sorting algorithms: the comparison function must be transitive. This affected not only scalar strings but also nested arrays and objects with string properties.
Solution
Added
EG(transitive_compare_mode)flag tozend_executor_globalsthat enforces transitive comparison during sorting operations, with consistent ordering that matches PHP 8+ semantics: empty-strings < numeric-types < numeric-strings < non-numeric.Modified three comparison functions to check this flag:
zendi_smart_strcmp()- handles string-to-string comparisonscompare_long_to_string()- handles integer-to-string comparisonscompare_double_to_string()- handles double-to-string comparisonsThe flag is properly initialized in
init_executor()and uses save/restore pattern inphp_array_data_compare_unstable_i()to handle reentrancy correctly.Important: This fix does not change the behavior of comparison operators like
<=>, maintaining backward compatibility. The fix only affects sorting and array operations with SORT_REGULAR.Tests
Added two comprehensive tests:
gh20262.phpt- Bug reproduction test covering scalars, objects, and nested arrayssort/sort_variation_numeric_strings.phpt- Edge case tests for numeric string handlingAll existing array sorting tests pass without modification!
Previous attempts:
This PR provides the complete solution.
Fixes #20262