-
Notifications
You must be signed in to change notification settings - 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
Closed
+662
−2
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c4fc6a9
Fix GH-20262: array_unique() SORT_REGULAR fails to deduplicate with m…
jmarble 6039897
array_unique: Security and performance improvements
jmarble 4482f7d
Fix array_unique to dereference values
jmarble 76d2f62
Remove duplicate code for complex type deduplication
jmarble File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
214 changes: 214 additions & 0 deletions
214
ext/standard/tests/array/array_unique_variation_sort_regular.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| --TEST-- | ||
| Test array_unique() function : SORT_REGULAR type coercion behavior | ||
| --FILE-- | ||
| <?php | ||
| echo "*** Testing array_unique() with SORT_REGULAR ***\n"; | ||
|
|
||
| // Test 1: Integer and string representations (coerce) | ||
| echo "\n-- Integer and string coercion --\n"; | ||
| var_dump(array_unique([1, "1", 2, "2"], SORT_REGULAR)); | ||
|
|
||
| // Test 2: Boolean coercion | ||
| echo "\n-- Boolean coercion --\n"; | ||
| var_dump(array_unique([true, 1, false, 0], SORT_REGULAR)); | ||
|
|
||
| // Test 3: NULL coercion with empty string and "0" | ||
| echo "\n-- NULL coercion --\n"; | ||
| var_dump(array_unique([null, "", false, 0, "0"], SORT_REGULAR)); | ||
|
|
||
| // Test 4: Float coercion | ||
| echo "\n-- Float coercion --\n"; | ||
| var_dump(array_unique([1, 1.0, "1", "1.0"], SORT_REGULAR)); | ||
|
|
||
| // Test 5: Numeric strings coerce | ||
| echo "\n-- Numeric strings --\n"; | ||
| var_dump(array_unique(["10", 10, "10.0", 10.0], SORT_REGULAR)); | ||
|
|
||
| // Test 6: Leading zeros make strings distinct | ||
| echo "\n-- Leading zeros --\n"; | ||
| var_dump(array_unique(["05", "5", 5], SORT_REGULAR)); | ||
|
|
||
| // Test 7: Partial numeric strings don't coerce | ||
| echo "\n-- Partial numeric strings --\n"; | ||
| var_dump(array_unique(["5abc", "5", 5], SORT_REGULAR)); | ||
|
|
||
| // Test 8: Whitespace in numeric strings | ||
| echo "\n-- Whitespace in numeric strings --\n"; | ||
| var_dump(array_unique(["5", " 5", "5 ", 5], SORT_REGULAR)); | ||
|
|
||
| // Test 9: Case sensitivity for non-numeric strings | ||
| echo "\n-- Case sensitivity --\n"; | ||
| var_dump(array_unique(["abc", "ABC", "Abc"], SORT_REGULAR)); | ||
|
|
||
| // Test 10: Exponential notation coerces | ||
| echo "\n-- Exponential notation --\n"; | ||
| var_dump(array_unique([1000, "1e3", "1000", 1e3], SORT_REGULAR)); | ||
|
|
||
| // Test 11: Negative numbers | ||
| echo "\n-- Negative numbers --\n"; | ||
| var_dump(array_unique([-5, "-5", -5.0], SORT_REGULAR)); | ||
|
|
||
| // Test 12: Arrays as values | ||
| echo "\n-- Arrays --\n"; | ||
| var_dump(array_unique([[1, 2], [1, 2], [1, 3]], SORT_REGULAR)); | ||
|
|
||
| // Test 13: NaN handling (NaN != NaN) | ||
| echo "\n-- NaN handling --\n"; | ||
| var_dump(array_unique([NAN, NAN, 1], SORT_REGULAR)); | ||
|
|
||
| // Test 14: INF handling | ||
| echo "\n-- INF handling --\n"; | ||
| var_dump(array_unique([INF, INF, -INF, -INF], SORT_REGULAR)); | ||
|
|
||
| // Test 15: Bug GH-20262 - mixed numeric and alphanumeric | ||
| echo "\n-- Bug GH-20262 case --\n"; | ||
| var_dump(array_unique(['5', '10', '3A', '5'], SORT_REGULAR)); | ||
|
|
||
| // Test 16: SORT_REGULAR vs SORT_STRING comparison | ||
| echo "\n-- SORT_REGULAR vs SORT_STRING --\n"; | ||
| $input = [true, 1, "1"]; | ||
| echo "SORT_REGULAR: "; | ||
| var_dump(array_unique($input, SORT_REGULAR)); | ||
| echo "SORT_STRING: "; | ||
| var_dump(array_unique($input, SORT_STRING)); | ||
|
|
||
| echo "\nDone\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| *** Testing array_unique() with SORT_REGULAR *** | ||
|
|
||
| -- Integer and string coercion -- | ||
| array(2) { | ||
| [0]=> | ||
| int(1) | ||
| [2]=> | ||
| int(2) | ||
| } | ||
|
|
||
| -- Boolean coercion -- | ||
| array(2) { | ||
| [0]=> | ||
| bool(true) | ||
| [2]=> | ||
| bool(false) | ||
| } | ||
|
|
||
| -- NULL coercion -- | ||
| array(2) { | ||
| [0]=> | ||
| NULL | ||
| [4]=> | ||
| string(1) "0" | ||
| } | ||
|
|
||
| -- Float coercion -- | ||
| array(1) { | ||
| [0]=> | ||
| int(1) | ||
| } | ||
|
|
||
| -- Numeric strings -- | ||
| array(1) { | ||
| [0]=> | ||
| string(2) "10" | ||
| } | ||
|
|
||
| -- Leading zeros -- | ||
| array(1) { | ||
| [0]=> | ||
| string(2) "05" | ||
| } | ||
|
|
||
| -- Partial numeric strings -- | ||
| array(2) { | ||
| [0]=> | ||
| string(4) "5abc" | ||
| [1]=> | ||
| string(1) "5" | ||
| } | ||
|
|
||
| -- Whitespace in numeric strings -- | ||
| array(1) { | ||
| [0]=> | ||
| string(1) "5" | ||
| } | ||
|
|
||
| -- Case sensitivity -- | ||
| array(3) { | ||
| [0]=> | ||
| string(3) "abc" | ||
| [1]=> | ||
| string(3) "ABC" | ||
| [2]=> | ||
| string(3) "Abc" | ||
| } | ||
|
|
||
| -- Exponential notation -- | ||
| array(1) { | ||
| [0]=> | ||
| int(1000) | ||
| } | ||
|
|
||
| -- Negative numbers -- | ||
| array(1) { | ||
| [0]=> | ||
| int(-5) | ||
| } | ||
|
|
||
| -- Arrays -- | ||
| array(2) { | ||
| [0]=> | ||
| array(2) { | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| [2]=> | ||
| array(2) { | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(3) | ||
| } | ||
| } | ||
|
|
||
| -- NaN handling -- | ||
| array(3) { | ||
| [0]=> | ||
| float(NAN) | ||
| [1]=> | ||
| float(NAN) | ||
| [2]=> | ||
| int(1) | ||
| } | ||
|
|
||
| -- INF handling -- | ||
| array(2) { | ||
| [0]=> | ||
| float(INF) | ||
| [2]=> | ||
| float(-INF) | ||
| } | ||
|
|
||
| -- Bug GH-20262 case -- | ||
| array(3) { | ||
| [0]=> | ||
| string(1) "5" | ||
| [1]=> | ||
| string(2) "10" | ||
| [2]=> | ||
| string(2) "3A" | ||
| } | ||
|
|
||
| -- SORT_REGULAR vs SORT_STRING -- | ||
| SORT_REGULAR: array(1) { | ||
| [0]=> | ||
| bool(true) | ||
| } | ||
| SORT_STRING: array(1) { | ||
| [0]=> | ||
| bool(true) | ||
| } | ||
|
|
||
| Done |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --TEST-- | ||
| Bug GH-20262 (array_unique() with SORT_REGULAR fails to remove duplicates with mixed strings) | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| // Original bug report case | ||
| $units = ['5', '10', '3A', '5']; | ||
| var_dump(array_unique($units, SORT_REGULAR)); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| array(3) { | ||
| [0]=> | ||
| string(1) "5" | ||
| [1]=> | ||
| string(2) "10" | ||
| [2]=> | ||
| string(2) "3A" | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.