Bug #80184 [Ver]: Complex expression in while / if statements resolves to false incorrectly

From: Date: Mon, 05 Oct 2020 09:17:17 +0000
Subject: Bug #80184 [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-229391@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: 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: Culprit is the block pass: $_main: ; (lines=22, args=0, vars=2, tmps=10) ; (before block pass) ; /home/nikic/php/php-src/t170.php:1-11 ; return [] RANGE[0..0] BB0: ; start lines=[0-9] ; to=(BB1) 0000 T2 = DECLARE_LAMBDA_FUNCTION string("") 0001 T3 = INIT_ARRAY 4 (packed) T2 NEXT 0002 T4 = DECLARE_LAMBDA_FUNCTION string("") 0003 T3 = ADD_ARRAY_ELEMENT T4 NEXT 0004 T5 = DECLARE_LAMBDA_FUNCTION string("") 0005 T3 = ADD_ARRAY_ELEMENT T5 NEXT 0006 T6 = DECLARE_LAMBDA_FUNCTION string("") 0007 T3 = ADD_ARRAY_ELEMENT T6 NEXT 0008 ASSIGN CV0($callbacks) T3 0009 NOP BB1: ; follow target lines=[10-14] ; to=(BB4, BB2) 0010 INIT_FCALL 1 96 string("array_shift") 0011 SEND_REF CV0($callbacks) 1 0012 V8 = DO_ICALL 0013 T9 = ASSIGN CV1($callback) V8 0014 T9 = JMPZ_EX T9 BB4 BB2: ; follow lines=[15-17] ; to=(BB1, BB3) 0015 INIT_DYNAMIC_CALL 0 CV1($callback) 0016 V10 = DO_FCALL 0017 T9 = JMPNZ_EX V10 BB1 BB3: ; follow lines=[18-20] ; to=(BB1, BB4) 0018 T11 = BOOL bool(true) 0019 T9 = BOOL T11 0020 JMPNZ T9 BB1 BB4: ; follow target exit lines=[21-21] 0021 RETURN int(1) $_main: ; (lines=17, args=0, vars=2, tmps=10) ; (after block pass) ; /home/nikic/php/php-src/t170.php:1-11 ; return [] RANGE[0..0] BB0: ; start lines=[0-13] ; to=(BB4, BB2) 0000 T2 = DECLARE_LAMBDA_FUNCTION string("") 0001 T3 = INIT_ARRAY 4 (packed) T2 NEXT 0002 T4 = DECLARE_LAMBDA_FUNCTION string("") 0003 T3 = ADD_ARRAY_ELEMENT T4 NEXT 0004 T5 = DECLARE_LAMBDA_FUNCTION string("") 0005 T3 = ADD_ARRAY_ELEMENT T5 NEXT 0006 T6 = DECLARE_LAMBDA_FUNCTION string("") 0007 T3 = ADD_ARRAY_ELEMENT T6 NEXT 0008 ASSIGN CV0($callbacks) T3 0009 INIT_FCALL 1 96 string("array_shift") 0010 SEND_REF CV0($callbacks) 1 0011 V8 = DO_ICALL 0012 T9 = ASSIGN CV1($callback) V8 0013 JMPZ T9 BB4 BB1: ; follow exit empty BB2: ; follow lines=[14-15] ; to=(BB1) 0014 INIT_DYNAMIC_CALL 0 CV1($callback) 0015 DO_FCALL BB4: ; target exit lines=[16-16] 0016 RETURN int(1) These are clearly not the same. Previous Comments: ------------------------------------------------------------------------ [2020-10-05 09:08:40] nikic@php.net 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#229391) next »