-
Couldn't load subscription status.
- Fork 8k
Fix GH-20262: array_unique() SORT_REGULAR fails to deduplicate with mixed strings #20273
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
Conversation
4102fe6 to
f60a45f
Compare
…h mixed strings array_unique() with SORT_REGULAR was failing to remove duplicate numeric strings when mixed with alphanumeric strings due to non-transitive comparison issues in the sort-based algorithm. Implemented hash-bucketing optimization for SORT_REGULAR that preserves full type coercion semantics while improving performance from O(n²) to O(n). Closes phpGH-20262
f60a45f to
c4fc6a9
Compare
- Eliminate Hash-DoS and integer overflow vulnerabilities - Add exception handling to prevent memory leaks - Implement type-specific optimizations (hash/sort/bucket) - Dynamic bucket sizing for memory efficiency - Fix resource handling
Use ZVAL_DEREF macro to dereference values before comparing in the array_unique function. Also add a new test case to verify the behavior.
|
Another variation of the example bug, but using objects: My PR does not make an attempt to resolve this issue with complex types. |
|
Closing in favor of #20305 which provides a complete solution that fixes transitivity for all mixed types (scalars, nested arrays, and objects). |
The Bug
array_unique()withSORT_REGULARwas failing to remove duplicate numeric strings when mixed with alphanumeric strings:Root Cause
Non-transitive comparisons in
SORT_REGULAR(where'5' == 5and5 != '5abc'but'5' < '5abc') broke the sort-based algorithm's assumption that sorting would group duplicates adjacently.Solution
Implemented type-optimized hybrid approach for
SORT_REGULAR:Three-tier algorithm selection:
Performance
Improved performance across all data types compared to PHP 8.4.13 while fixing correctness issues.
Backward Compatibility
Tests Added
gh20262.phpt- Minimal regression test for the bugarray_unique_variation_sort_regular.phpt- Comprehensive SORT_REGULAR behavior coverage (16 scenarios)