Skip to content

Conversation

@jmarble
Copy link

@jmarble jmarble commented Oct 28, 2025

Problem

array_unique() with SORT_REGULAR was missing duplicates, and sort() 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 cycle

This 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 to zend_executor_globals that 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 comparisons
  • compare_long_to_string() - handles integer-to-string comparisons
  • compare_double_to_string() - handles double-to-string comparisons

The flag is properly initialized in init_executor() and uses save/restore pattern in php_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 arrays
  • sort/sort_variation_numeric_strings.phpt - Edge case tests for numeric string handling

All existing array sorting tests pass without modification!


Previous attempts:

This PR provides the complete solution.

Fixes #20262

@jmarble
Copy link
Author

jmarble commented Oct 28, 2025

@devnexen @nielsdos I reached out to @nikic regarding the four failed tests.

Update: Fixed! The tests were failing because:

  1. Empty strings weren't being handled as a special case (they should sort before numbers)
  2. The transitive_compare_mode flag wasn't initialized (caused issues on Windows ZTS)

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.
@jmarble jmarble force-pushed the fix-sort-regular-transitivity-tls branch from 7f9704c to 438b127 Compare October 28, 2025 22:09
@jmarble jmarble marked this pull request as ready for review October 28, 2025 22:45
jmarble and others added 3 commits October 29, 2025 10:58
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().
@jmarble jmarble requested a review from dstogov as a code owner October 29, 2025 21:13
Copy link
Member

@withinboredom withinboredom left a 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 promotions

Suggested-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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

array_unique() with SORT_REGULAR returns duplicate values

2 participants