Commit ad5bca36e54 for php

commit ad5bca36e544e36f6f3980602d6e52a19af6a774
Author: ChrisAbner <chrisabner@hotmail.com>
Date:   Sun Dec 6 19:23:50 2026 +0100

    Fix optimizer turning $i++ < $n into ++$i <= $n for non-integer operands (GH-25606)

    The DFA pass rewrote $i++ < $n as ++$i <= $n (and $n < $i-- as
    $n <= --$i) whenever the counter was int or float, without looking at
    the other operand. The two forms are only equivalent for integers that
    do not overflow. With a float, numeric string, null or bool bound, or a
    float counter, the loop ran one iteration less. With $n = PHP_INT_MAX
    the optimized loop never ended.

    Only rewrite when the counter, the incremented value and the other
    operand are all int. Type inference already marks the incremented
    value as int|float when it may overflow.

    Closes GH-25606

diff --git a/NEWS b/NEWS
index 62fed60730c..8d12b3e0145 100644
--- a/NEWS
+++ b/NEWS
@@ -43,6 +43,8 @@ PHP                                                                        NEWS
     branches. (ilutov, Arnaud)
   . Fixed missing "Undefined variable" warning for a dead array literal
     element removed by DCE. (fQwQf)
+  . Fixed $i++ < $n being optimized to ++$i <= $n for non-integer operands or
+    on integer overflow. (ChrisAbner)

 - PCRE:
   . Fixed bug GH-26100 (Cross-request use-after-free of the cached subpatterns
diff --git a/Zend/Optimizer/dfa_pass.c b/Zend/Optimizer/dfa_pass.c
index 2ad444a4a91..ee78e1b08cd 100644
--- a/Zend/Optimizer/dfa_pass.c
+++ b/Zend/Optimizer/dfa_pass.c
@@ -1435,11 +1435,13 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
 					int result_var = ssa->ops[op_1].result_def;

 					if (result_var >= 0
-					 && (ssa->var_info[result_var].type & ((MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_DOUBLE))) == 0) {
+					 && (ssa->var_info[result_var].type & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_LONG
+					 && (ssa->var_info[v].type & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_LONG) {
 						int use = ssa->vars[result_var].use_chain;

 						if (use >= 0 && op_array->opcodes[use].opcode == ZEND_IS_SMALLER
 						 && ssa->ops[use].op1_use == result_var
+						 && (_ssa_op2_info(op_array, ssa, &op_array->opcodes[use], &ssa->ops[use]) & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_LONG
 						 && zend_dfa_try_to_replace_result(op_array, ssa, op_1, v)) {
 							opline->opcode = ZEND_PRE_INC;
 							op_array->opcodes[use].opcode = ZEND_IS_SMALLER_OR_EQUAL;
@@ -1449,11 +1451,13 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
 					int result_var = ssa->ops[op_1].result_def;

 					if (result_var >= 0
-					 && (ssa->var_info[result_var].type & ((MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF) - (MAY_BE_LONG|MAY_BE_DOUBLE))) == 0) {
+					 && (ssa->var_info[result_var].type & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_LONG
+					 && (ssa->var_info[v].type & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_LONG) {
 						int use = ssa->vars[result_var].use_chain;

 						if (use >= 0 && op_array->opcodes[use].opcode == ZEND_IS_SMALLER
 						 && ssa->ops[use].op2_use == result_var
+						 && (_ssa_op1_info(op_array, ssa, &op_array->opcodes[use], &ssa->ops[use]) & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) == MAY_BE_LONG
 						 && zend_dfa_try_to_replace_result(op_array, ssa, op_1, v)) {
 							opline->opcode = ZEND_PRE_DEC;
 							op_array->opcodes[use].opcode = ZEND_IS_SMALLER_OR_EQUAL;
diff --git a/ext/opcache/tests/opt/inc_dec_is_smaller.phpt b/ext/opcache/tests/opt/inc_dec_is_smaller.phpt
new file mode 100644
index 00000000000..1d81e84bd11
--- /dev/null
+++ b/ext/opcache/tests/opt/inc_dec_is_smaller.phpt
@@ -0,0 +1,120 @@
+--TEST--
+$i++ < $n must not be optimized to ++$i <= $n unless both operands are integers that cannot overflow
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.optimization_level=-1
+--FILE--
+<?php
+
+function half(array $a) {
+    $n = count($a) / 2;
+    $i = 0;
+    $o = [];
+    while ($i++ < $n) {
+        $o[] = $a[$i - 1];
+    }
+    return $o;
+}
+
+function float_bound(float $n) {
+    $c = 0;
+    for ($i = 0; $i++ < $n;) $c++;
+    return $c;
+}
+
+function numeric_string_bound(string $n) {
+    $c = 0;
+    for ($i = 0; $i++ < $n;) $c++;
+    return $c;
+}
+
+function float_counter() {
+    $c = 0;
+    $x = 0.5;
+    while ($x++ < 3) $c++;
+    return $c;
+}
+
+function null_bound(int $i, ?int $n) {
+    return $i++ < $n;
+}
+
+function bool_bound(int $i, bool $n) {
+    return $i++ < $n;
+}
+
+function post_dec_float_bound(float $min) {
+    $c = 0;
+    $i = 3;
+    while ($min < $i--) $c++;
+    return $c;
+}
+
+function inc_overflow(int $i, int $n) {
+    $c = 0;
+    while ($i++ < $n && $c < 5) $c++;
+    return $c;
+}
+
+function dec_overflow(int $i, int $n) {
+    $c = 0;
+    while ($n < $i-- && $c < 5) $c++;
+    return $c;
+}
+
+function int_bound(int $n) {
+    $c = 0;
+    for ($i = 0; $i++ < $n;) $c++;
+    return $c;
+}
+
+function literal_bound() {
+    $c = 0;
+    for ($i = 0; $i++ < 3;) $c++;
+    return $c;
+}
+
+function post_dec_int_bound(int $min) {
+    $c = 0;
+    $i = 3;
+    while ($min < $i--) $c++;
+    return $c;
+}
+
+var_dump(half([1, 2, 3, 4, 5]));
+var_dump(float_bound(2.5));
+var_dump(numeric_string_bound("2.5"));
+var_dump(float_counter());
+var_dump(null_bound(-1, null));
+var_dump(bool_bound(-1, false));
+var_dump(post_dec_float_bound(0.5));
+var_dump(inc_overflow(PHP_INT_MAX - 1, PHP_INT_MAX));
+var_dump(dec_overflow(PHP_INT_MIN + 1, PHP_INT_MIN));
+var_dump(int_bound(3));
+var_dump(literal_bound());
+var_dump(post_dec_int_bound(0));
+
+?>
+--EXPECT--
+array(3) {
+  [0]=>
+  int(1)
+  [1]=>
+  int(2)
+  [2]=>
+  int(3)
+}
+int(3)
+int(3)
+int(3)
+bool(false)
+bool(false)
+int(3)
+int(1)
+int(1)
+int(3)
+int(3)
+int(3)