Bug #80184 [Opn->Ver]: Complex expression in while / if statements resolves to false incorrectly
| From: | nikic@php.net | Date: | Mon, 05 Oct 2020 09:08:40 +0000 |
| Subject: | Bug #80184 [Opn->Ver]: Complex expression in while / if statements resolves to false incorrectly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229389@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80184&edit=1
ID: 80184
Updated by: nikic@php.net
Reported by: jelly dot legend at gmail dot com
Summary: Complex expression in while / if statements resolves
to false incorrectly
-Status: Open
+Status: Verified
Type: Bug
Package: opcache
Operating System: MacOS 10.15.6
PHP Version: 8.0.0rc1
Block user comment: N
Private report: N
New Comment:
Also happens with opcache.jit=0, so generic optimization issue.
Slightly reduced by Dharman:
<?php
$callbacks = [
function () { echo "First item!\n"; },
function () { echo "Second item!\n"; },
function () { echo "Third item!\n"; },
function () { echo "Fourth item!\n"; },
];
while ($callback = array_shift($callbacks) and $callback() || true);
This iterates once instead of multiple times.
Previous Comments:
------------------------------------------------------------------------
[2020-10-04 12:58:24] jelly dot legend at gmail dot com
Description:
------------
If a use a complex expression as an if condition or while condition, it incorrectly resolves to
false. While it correctly evaluates it if passed to a variable and printed.
PHP version:
PHP 8.0.0rc1 (cli) (built: Oct 4 2020 13:58:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.0.0rc1, Copyright (c), by Zend Technologies
php.development-ini changes:
zend_extension=opcache
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
If I disable opcache I get the correct results.
PHP was built from source. With following configuration.
./configure \
--prefix=.../php-8.0.0rc1 \
--with-config-file-path=.../php-8.0.0rc1/etc \
--enable-mbstring \
--enable-sockets \
--enable-opcache \
--with-openssl=/usr/local/openssl \
--with-curl \
--with-sodium \
--with-bz2 \
--with-zlib \
--with-zip
I was first noticed the bug on beta-4 but decided to try it out in rc-1 instead.
Test script:
---------------
$factory = function (string $msg): Closure {
return function () use ($msg): void {
echo $msg;
};
};
$callbacks = [
$factory('First item!'),
$factory('Second item!'),
$factory('Third item!'),
$factory('Fourth item!'),
];
$nl = function (): void {
echo PHP_EOL;
};
while ((null !== $callback = array_shift($callbacks)) && ($callback() || $nl() || true))
/**/; // exists after first item, should iterate thought all
// Full test at: https://pastebin.com/br54nGvM
Expected result:
----------------
First item!
Second item!
Third item!
Fourth item!
Actual result:
--------------
First item!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80184&edit=1