Bug #81015 [NEW]: Opcache optimization assumes wrong part of ternary operator in if-condition

From: Date: Thu, 06 May 2021 04:14:42 +0000
Subject: Bug #81015 [NEW]: Opcache optimization assumes wrong part of ternary operator in if-condition
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233709@lists.php.net to get a copy of this message
From:             redwormik at gmail dot com
Operating system: Alpine 3.13 (Docker)
PHP version:      8.0.5
Package:          opcache
Bug Type:         Bug
Bug description:Opcache optimization assumes wrong part of ternary operator in if-condition

Description:
------------
I have opcache enabled and opcache.optimization_level at least
0x000000b0 (bits 4, 5 and 8 all set, other irrelevant).

When I compare a variable to NULL in a "false" part of a ternary
operator in an if-condition, the variable changes to NULL in the
respective branch of the if-condition (i.e. if comparison is === NULL,
the variable changes to NULL in the if-branch; if comparison is !==
NULL, the variable changes to NULL in the else-branch), even though the
ternary operator condition is true.

Returning the value, changing the condition so it does not use ternary
operator, moving the NULL-equality out of the condition to a local
variable or comparing to anything else other than NULL all work as
expected.

Test script:
---------------
<?php
declare(strict_types=1);

function ternary(bool $enabled, ?string $value): void
{
	// the "true" part is not as trivial in the real case
	if ($enabled ? true : $value === null) {
		echo ($value ?? 'NULL') . "\n";
	} else {
		echo "INVALID\n";
	}
}

ternary(true, 'value');
ternary(true, null);
ternary(false, 'value');
ternary(false, null);


Expected result:
----------------
value
NULL
INVALID value
NULL


Actual result:
--------------
NULL
NULL
INVALID value
NULL

-- 
Edit bug report at https://bugs.php.net/bug.php?id=81015&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81015&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81015&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81015&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81015&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81015&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81015&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81015&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81015&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81015&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81015&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81015&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81015&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81015&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81015&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81015&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81015&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81015&r=mysqlcfg


Thread (5 messages)

« previous php.bugs (#233709) next »