Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Zend/Optimizer/dfa_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,8 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
&& Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG
&& Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1
&& ssa->ops[op_2].op1_use >= 0
&& !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {
/* MAY_BE_UNDEF|MAY_BE_NULL is excluded because of the currently inconsistent behaviour, revisit in PHP 9 once it is fixed */
&& !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {

op_array->opcodes[op_2].opcode = ZEND_PRE_DEC;
SET_UNUSED(op_array->opcodes[op_2].op2);
Expand Down Expand Up @@ -1513,7 +1514,8 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
&& Z_TYPE_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == IS_LONG
&& Z_LVAL_P(CT_CONSTANT_EX(op_array, op_array->opcodes[op_2].op2.constant)) == 1
&& ssa->ops[op_2].op1_use >= 0
&& !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {
/* MAY_BE_UNDEF|MAY_BE_NULL is excluded because of the currently inconsistent behaviour, revisit in PHP 9 once it is fixed */
&& !(ssa->var_info[ssa->ops[op_2].op1_use].type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF))) {

op_array->opcodes[op_2].opcode = ZEND_PRE_DEC;
SET_UNUSED(op_array->opcodes[op_2].op2);
Expand Down
43 changes: 43 additions & 0 deletions ext/opcache/tests/opt/gh25061.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
GH-25061 ($x = $x - 1 on null is optimized to PRE_DEC)
--EXTENSIONS--
opcache
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
--FILE--
<?php

function nullable(?int $x)
{
$x = $x - 1;
return $x;
}

function undef()
{
$x = $x - 1;
return $x;
}

function twice(?int $x)
{
$x = $x - 1;
$x = $x - 1;
return $x;
}

var_dump(nullable(null));
var_dump(nullable(5));
var_dump(undef());
var_dump(twice(null));

?>
--EXPECTF--
int(-1)
int(4)

Warning: Undefined variable $x in %s on line %d
int(-1)
int(-2)
Loading