Skip to content

Stdlib: sort() on float arrays with NAN — LogicException not Zend sort order (ext/standard/array.c) #10144

Closed

Description

@PurHur

Category

stdlib · php-src-strict

Problem

sort($arr, SORT_REGULAR) on arrays containing float values (including NAN) must use php-src comparison semantics. VM throws LogicException: sort() only supports homogeneous string or integer arrays instead of sorting.

php-src reference

Repro (failure today — verified 2026-06-19)

test/repro/sort_float_nan.php:

<?php
$b = [3.0, NAN, 1.0];
sort($b, SORT_REGULAR);
var_export($b);
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/sort_float_nan.php
php bin/vm.php test/repro/sort_float_nan.php'
Call Zend PHP 8.2 VM today
sort([3.0, NAN, 1.0]) array(0 => NAN, 1 => 1.0, 2 => 3.0) LogicException ❌

Scope (PHP-in-PHP first)

Path Work
ext/standard/VmArray.php Allow float/double sort path; delegate to shared compare
Sort helpers under ext/standard/ NaN-aware <=> for SORT_REGULAR
rsort/usort/uasort Audit float+NAN siblings
Tests test/compliance/cases/stdlib/sort_float_nan.phpt

Remove or narrow the homogeneous int/string guard — php-src sorts mixed numeric arrays.

Done when (php-src-strict)

  • Repro sorts without exception; element order matches Zend
  • rsort on same input verified
  • Pure int/string sort regressions unchanged
  • JIT agrees when sort is JIT-lowering in scope

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php test/repro/sort_float_nan.php'

Related

Activity

  1. added
    enhancementNew feature or request
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 19, 2026
  2. PurHur commented on Jul 4, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-agent-lane-B — starting this run

  3. PurHur commented on Jul 4, 2026

    @PurHur
    OwnerAuthor

    Merged via #15788.

    Fix: NaN-aware numeric compare in VmInternalCompare::compareNumericScalarsForSort() — ascending sort places NaN before finite values; descending keeps NaN slots stable (php-src parity).

    Verification (docker-exec):

    • php test/repro/sort_float_nan.php / php bin/vm.php test/repro/sort_float_nan.php → both print [NAN, 1.0, 3.0] then [3.0, NAN, 1.0]
    • vendor/bin/phpunit --filter sort_float_nan → OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions