Skip to content

The Not ! operator in combination with NAN fails with OPcache enabled #15773

Open

Description

@jsiefer

Description

The following code:

$a = 100.0;
$b = \NAN;

$cmp = $a > $b; // since NAN always false;
$cmp2 = !($a > $b); // $expected true;

var_dump($cmp);
var_dump($cmp2);

assert($cmp === false);
assert($cmp2 === true);

Resulted in this output:

bool(false)
bool(true)

Resulted in this output with opcache enabled:

bool(false)
bool(false) ❌

But I expected this output instead:

bool(false)
bool(true)

The issue only seems to appear using opcache. Also spotted on different environments with older PHP versions.

PHP 8.3.11 (cli) (built: Aug 30 2024 16:21:34) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.11, Copyright (c) Zend Technologies
    with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans
    with Zend OPcache v8.3.11, Copyright (c), by Zend Technologies

PHP Version

PHP 8.3.11

Operating System

No response

Activity

  1. cmb69 commented on Sep 6, 2024

    @cmb69
    Member

    This is likely due to the optimizer converting !($a > $b) to $a <= $b.

    If I remember correctly, this has been reported before. Well, might have had https://bugs.php.net/bug.php?id=80701 in mind.

  2. cmb69 commented on Sep 6, 2024

    @cmb69
    Member

    This is likely due to the optimizer converting !($a > $b) to $a <= $b.

    Indeed (simplified script):

         ; (lines=6, args=0, vars=3, tmps=5)
         ; (before optimizer)
         ; C:\php-sdk\phpdev\vs17\x64\gh15773.php:1-13
         ; return  [] RANGE[0..0]
    0000 ASSIGN CV0($a) float(100)
    0001 ASSIGN CV1($b) float(nan)
    0002 T5 = IS_SMALLER CV1($b) CV0($a)
    0003 T6 = BOOL_NOT T5
    0004 ASSIGN CV2($cmp2) T6
    0005 RETURN int(1)
    
    $_main:
         ; (lines=5, args=0, vars=3, tmps=1)
         ; (after optimizer)
         ; C:\php-sdk\phpdev\vs17\x64\gh15773.php:1-13
    0000 ASSIGN CV0($a) float(100)
    0001 ASSIGN CV1($b) float(nan)
    0002 T3 = IS_SMALLER_OR_EQUAL CV0($a) CV1($b)
    0003 ASSIGN CV2($cmp2) T3
    0004 RETURN int(1)
    

    Might be wontfix, since the optimization is generally useful, and NaN is very special anyway. Maybe PHP should throw on any NaN comparisons.

  3. jsiefer commented on Sep 6, 2024

    @jsiefer
    Author

    I found it super mean to debug, but I am with you on the performance optimization.

    Maybe its possible to show warnings if given pattern is used. Might be something for static type checkers.... just thinking loud

  4. cmb69 commented on Sep 6, 2024

    @cmb69
    Member

    Well, actually NaN is a somewhat special case of incomparable values, and the [documentation](language.operators.comparison.incomparable states:

    While identity comparison (=== and !==) can be applied to arbitrary values, the other comparison operators should only be applied to comparable values. The result of comparing incomparable values is undefined, and should not be relied upon.

    That's the easy way out.

    Maybe its possible to show warnings if given pattern is used.

    Yeah, a warning would be less intrusive than throwing, but would also be a BC break. It seems to me that this warrants the RFC process.

    Might be something for static type checkers.

    A static type checker may not be able to detect all cases, especially as NaN is a float, and can be the result of a couple of operations.

  5. changed the title [-]The Not ! opperator in componation with NAN fails with OPcache enabled[/-] [+]The Not ! operator in combination with NAN fails with OPcache enabled[/+] on Jan 28, 2025
  6. mvalasis commented on Nov 22, 2026

    @mvalasis

    Still reproduces on PHP 8.5.8 and 8.6.0 (CLI, -n, opcache built in):

    <?php
    function lt_neg( float $x ): bool { return ! ( $x < 1.0 ); }
    function gt_neg( float $x ): bool { return ! ( $x > 1.0 ); }
    function le_neg( float $x ): bool { return ! ( $x <= 1.0 ); }
    function if_neg( float $x ): string { if ( ! ( $x < 1.0 ) ) { return 'guarded'; } return 'through'; }
    function mixed_neg( $a, $b ): bool { return ! ( $a < $b ); }
    echo json_encode( array( lt_neg( NAN ), gt_neg( NAN ), le_neg( NAN ), if_neg( NAN ), mixed_neg( NAN, 1.0 ) ) ), "\n";
    $ php -n a.php
    [true,true,true,"guarded",true]
    $ php -n -d opcache.enable_cli=1 -d opcache.file_update_protection=0 a.php
    [false,false,false,"through",false]
    $ php -n -d opcache.enable_cli=1 -d opcache.file_update_protection=0 -d opcache.optimization_level=0x7FFEBFEF a.php
    [true,true,true,"guarded",true]
    
    • Clearing only bit 0x10 (pass 5, the CFG block pass) from the default 0x7FFEBFFF gives the unoptimized result back.
    • !($x < y), !($x > y) and !($x <= y) are all affected. That holds for a returned value and for an if condition, and with untyped operands too.
    • With the default opcache.file_update_protection=2, a script modified in the last 2 s is not cached, so it is not optimized. Running a repro right after saving it prints the correct result, which looks like a fix.
    • opcache.enable_cli defaults to 0, so a CLI test suite runs such code unoptimized and passes. We had a guard written as if ( ! ( abs( $x ) < 1e12 ) ) that was meant to send NAN and huge values to a float path. Under opcache NAN skipped that path and reached an (int) cast. The guard is now written is_nan( $x ) || abs( $x ) >= 1e12.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions