php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Wrong type inference of range() result

Open MGlaus opened this issue 4 years ago • 2 comments

Description

I got a strange one :)

There is an inconsistency between php-cgi and php-cli if following conditions are meet.

  • Generate an array with range() where one argument is a string
  • Use this array in a foreach-Loop inside a function
  • Do maths with the value of an array entry inside the foreach-Loop
  • Extension opcache installed and enabled

If the code is run with php-cgi, the value is empty in the math expression. The cli version seems to work as intended.

The following code:

<?php
        function test() {
                $array = range(1,"2");

                foreach ($array as $i) {
                        var_dump($i + 1);
                }
        }

        test();

Resulted in this output (only for php-cgi):

float(1)
float(1)

But I expected this output instead:

int(2)
int(3)

PHP Version

PHP 8.0.15, PHP 8.1.2

Operating System

No response

MGlaus avatar Feb 10 '22 16:02 MGlaus

I can confirm this (without JIT; haven't checked with).

cmb69 avatar Feb 14 '22 11:02 cmb69

This is not really related to the SAPI, but rather an optimization issue:

> php -dopcache.opt_debug_level=0x50 ..\gh8074.php
$_main:
     ; (lines=3, args=0, vars=0, tmps=1)
     ; (after pass 5)
     ; C:\php-sdk\phpdev\vs16\x64\gh8074.php:1-10
     ; return  [] RANGE[0..0]
0000 INIT_FCALL 0 192 string("test")
0001 DO_UCALL
0002 RETURN int(1)

test:
     ; (lines=14, args=0, vars=2, tmps=5)
     ; (after pass 5)
     ; C:\php-sdk\phpdev\vs16\x64\gh8074.php:2-8
     ; return  [] RANGE[0..0]
0000 INIT_FCALL 2 112 string("range")
0001 SEND_VAL int(1) 1
0002 SEND_VAL string("2") 2
0003 V2 = DO_ICALL
0004 ASSIGN CV0($array) V2
0005 V4 = FE_RESET_R CV0($array) 0012
0006 FE_FETCH_R V4 CV1($i) 0012
0007 INIT_FCALL 1 96 string("var_dump")
0008 T5 = ADD CV1($i) int(1)
0009 SEND_VAL T5 1
0010 DO_ICALL
0011 JMP 0006
0012 FE_FREE V4
0013 RETURN null

$_main:
     ; (lines=3, args=0, vars=0, tmps=0, no_loops)
     ; (after pass 7)
     ; C:\php-sdk\phpdev\vs16\x64\gh8074.php:1-10
     ; return  [long] RANGE[1..1]
0000 INIT_FCALL 0 192 string("test")
0001 DO_UCALL
0002 RETURN int(1)

test:
     ; (lines=14, args=0, vars=2, tmps=2)
     ; (after pass 7)
     ; C:\php-sdk\phpdev\vs16\x64\gh8074.php:2-8
     ; return  [null] RANGE[0..0]
0000 INIT_FCALL 2 112 string("range")
0001 SEND_VAL int(1) 1
0002 SEND_VAL string("2") 2
0003 V2 = DO_ICALL
0004 CV0($array) = QM_ASSIGN V2
0005 V2 = FE_RESET_R CV0($array) 0012
0006 FE_FETCH_R V2 CV1($i) 0012
0007 INIT_FCALL 1 96 string("var_dump")
0008 T3 = ADD CV1($i) float(1)
0009 SEND_VAL T3 1
0010 DO_ICALL
0011 JMP 0006
0012 FE_FREE V2
0013 RETURN null
float(1)
float(1)

cmb69 avatar Feb 18 '22 16:02 cmb69