Commit 4cdbae3db38 for php
commit 4cdbae3db380a644fbd729e95470974f06b16ba8
Author: fQwQf <supertjz123@foxmail.com>
Date: Sun Dec 6 18:40:37 2026 +0100
Zend/Optimizer: Keep dead ADD_ARRAY_ELEMENT reading undefined CVs in DCE (GH-26055)
The may_throw_dce_exception() exemption ignored zend_may_throw() for every
ADD_ARRAY_ELEMENT append, so DCE removed appends whose op1 CV may be
undefined and dropped the "Undefined variable" warning.
Don't apply the exemption when op1 is a CV that may be undefined.
Closes GH-26055
diff --git a/NEWS b/NEWS
index b1840b1f3f2..8ba3ccdbef8 100644
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,8 @@ PHP NEWS
(David Carlier)
. Fixed type inference of JIT trace after FE_FETCH_R[W] with identical
branches. (ilutov, Arnaud)
+ . Fixed missing "Undefined variable" warning for a dead array literal
+ element removed by DCE. (fQwQf)
- PCRE:
. Fixed bug GH-26100 (Cross-request use-after-free of the cached subpatterns
diff --git a/Zend/Optimizer/dce.c b/Zend/Optimizer/dce.c
index 88164671af8..35bb3bcafff 100644
--- a/Zend/Optimizer/dce.c
+++ b/Zend/Optimizer/dce.c
@@ -520,8 +520,10 @@ static inline bool may_break_varargs(const zend_op_array *op_array, const zend_s
return 0;
}
-static inline bool may_throw_dce_exception(const zend_op *opline) {
- return opline->opcode == ZEND_ADD_ARRAY_ELEMENT && opline->op2_type == IS_UNUSED;
+static inline bool may_throw_dce_exception(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_ssa *ssa) {
+ return opline->opcode == ZEND_ADD_ARRAY_ELEMENT
+ && opline->op2_type == IS_UNUSED
+ && !(opline->op1_type == IS_CV && (ssa->var_info[ssa_op->op1_use].type & MAY_BE_UNDEF));
}
int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer_ctx, zend_ssa *ssa, bool reorder_dtor_effects) {
@@ -588,7 +590,7 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *optimizer
}
} else if (may_have_side_effects(op_array, ssa, &op_array->opcodes[i], &ssa->ops[i], ctx.reorder_dtor_effects)
|| (zend_may_throw(&op_array->opcodes[i], &ssa->ops[i], op_array, ssa)
- && !may_throw_dce_exception(&op_array->opcodes[i]))
+ && !may_throw_dce_exception(&op_array->opcodes[i], &ssa->ops[i], ssa))
|| (has_varargs && may_break_varargs(op_array, ssa, &ssa->ops[i]))) {
if (op_array->opcodes[i].opcode == ZEND_NEW
&& op_array->opcodes[i+1].opcode == ZEND_DO_FCALL
diff --git a/ext/opcache/tests/opt/dce_add_array_element_undef_cv.phpt b/ext/opcache/tests/opt/dce_add_array_element_undef_cv.phpt
new file mode 100644
index 00000000000..6075bf437f2
--- /dev/null
+++ b/ext/opcache/tests/opt/dce_add_array_element_undef_cv.phpt
@@ -0,0 +1,20 @@
+--TEST--
+DCE must not remove dead ADD_ARRAY_ELEMENT reading an undefined variable
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=0x7FFEBFFF
+opcache.jit=0
+--EXTENSIONS--
+opcache
+--FILE--
+<?php
+set_error_handler(function($no, $str) { echo "H:$str\n"; return true; });
+function f() { $r = [$x, $y]; return 1; }
+function g() { $r = [1, $y]; return 1; }
+f();
+g();
+--EXPECT--
+H:Undefined variable $x
+H:Undefined variable $y
+H:Undefined variable $y