Commit 85d2cd65e1b for php

commit 85d2cd65e1b726c378db1a4294cff97d780a336d
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date:   Sun Nov 22 12:43:45 2026 +0100

    ext/opcache: Fix JIT ASSIGN_DIM undefined OP_DATA and dim reporting on non-array containers (GH-24665)

    Report an undefined OP_DATA only when the value is assigned, after the
    false to array deprecation, and never report an undefined dim before
    "Cannot use a scalar value as an array", matching the VM.

    Closes GH-24665

diff --git a/NEWS b/NEWS
index f90e69fa164..02558b5a277 100644
--- a/NEWS
+++ b/NEWS
@@ -118,6 +118,8 @@ PHP                                                                        NEWS
     (pavimus)
   . Fixed incorrect constant propagation when unpacking into partially known
     arrays. (Weilin Du)
+  . Fixed function JIT ASSIGN_DIM reporting an undefined value or key out of
+    order, or when the VM does not, on non-array containers. (Jorg Sowa)

 - PDO_Firebird:
   . Fixed stack buffer overflow with long connection user names, passwords,
diff --git a/ext/opcache/jit/zend_jit_helpers.c b/ext/opcache/jit/zend_jit_helpers.c
index 0579f1d2592..acf25d05e3b 100644
--- a/ext/opcache/jit/zend_jit_helpers.c
+++ b/ext/opcache/jit/zend_jit_helpers.c
@@ -1538,13 +1538,6 @@ static void ZEND_FASTCALL zend_jit_assign_dim_helper(zval *object_ptr, zval *dim
 		return;
 	}

-	if (UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
-		const zend_op *op_data = EG(current_execute_data)->opline + 1;
-		ZEND_ASSERT(op_data->opcode == ZEND_OP_DATA && op_data->op1_type == IS_CV);
-		zend_jit_undefined_op_helper(op_data->op1.var);
-		value = &EG(uninitialized_zval);
-	}
-
 	if (EXPECTED(Z_TYPE_P(object_ptr) == IS_STRING)) {
 		zend_throw_error(NULL, "[] operator not supported for strings");
 		if (result) {
@@ -1574,16 +1567,30 @@ static void ZEND_FASTCALL zend_jit_assign_dim_helper(zval *object_ptr, zval *dim
 			return;
 		}

+		if (UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
+			/* EX(opline) no longer points to ASSIGN_DIM after a throw; the VM skips the warning too. */
+			if (EXPECTED(!EG(exception))) {
+				const zend_op *op_data = EG(current_execute_data)->opline + 1;
+				ZEND_ASSERT(op_data->opcode == ZEND_OP_DATA && op_data->op1_type == IS_CV);
+				if (!zend_jit_undefined_op_helper_write(arr, op_data->op1.var)) {
+					if (result) {
+						if (EG(exception)) {
+							ZVAL_UNDEF(result);
+						} else {
+							ZVAL_NULL(result);
+						}
+					}
+					return;
+				}
+			}
+			value = &EG(uninitialized_zval);
+		}
+
 		ZVAL_COPY_DEREF(var, value);
 		if (result) {
 			ZVAL_COPY(result, var);
 		}
 	} else {
-		if (dim && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
-			const zend_op *opline = EG(current_execute_data)->opline;
-			zend_jit_undefined_op_helper(opline->op2.var);
-			dim = &EG(uninitialized_zval);
-		}
 		zend_throw_error(NULL, "Cannot use a scalar value as an array");
 		if (result) {
 			ZVAL_UNDEF(result);
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_001.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_001.phpt
new file mode 100644
index 00000000000..c60b9e6f867
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_001.phpt
@@ -0,0 +1,21 @@
+--TEST--
+JIT ASSIGN_DIM: undefined OP_DATA is not read when the container is a scalar
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+function test() {
+    $x = 1.5;
+    $x[0] = $undef;
+}
+try {
+    test();
+} catch (Error $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+Error: Cannot use a scalar value as an array
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_002.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_002.phpt
new file mode 100644
index 00000000000..56ce424c5d7
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_002.phpt
@@ -0,0 +1,21 @@
+--TEST--
+JIT ASSIGN_DIM: undefined dim is not reported when the container is a scalar
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+function test() {
+    $x = 1.5;
+    $x[$undef] = 1;
+}
+try {
+    test();
+} catch (Error $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+Error: Cannot use a scalar value as an array
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_003.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_003.phpt
new file mode 100644
index 00000000000..3ce0bdd9e77
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_003.phpt
@@ -0,0 +1,21 @@
+--TEST--
+JIT ASSIGN_DIM: undefined OP_DATA is not read when appending to a string
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+function test() {
+    $x = 'ab';
+    $x[] = $undef;
+}
+try {
+    test();
+} catch (Error $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+Error: [] operator not supported for strings
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_004.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_004.phpt
new file mode 100644
index 00000000000..a793dd6b316
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_004.phpt
@@ -0,0 +1,24 @@
+--TEST--
+JIT ASSIGN_DIM: undefined OP_DATA is reported after the false to array deprecation
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+function test() {
+    $x = false;
+    $x[0] = $undef;
+    var_dump($x);
+}
+test();
+?>
+--EXPECTF--
+Deprecated: Automatic conversion of false to array is deprecated in %s on line %d
+
+Warning: Undefined variable $undef in %s on line %d
+array(1) {
+  [0]=>
+  NULL
+}
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_005.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_005.phpt
new file mode 100644
index 00000000000..1246cb6b5ba
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_005.phpt
@@ -0,0 +1,24 @@
+--TEST--
+JIT ASSIGN_DIM: undefined OP_DATA is reported after the false to array deprecation when appending
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+function test() {
+    $x = false;
+    $x[] = $undef;
+    var_dump($x);
+}
+test();
+?>
+--EXPECTF--
+Deprecated: Automatic conversion of false to array is deprecated in %s on line %d
+
+Warning: Undefined variable $undef in %s on line %d
+array(1) {
+  [0]=>
+  NULL
+}
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_006.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_006.phpt
new file mode 100644
index 00000000000..2bc232fd1b4
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_006.phpt
@@ -0,0 +1,26 @@
+--TEST--
+JIT ASSIGN_DIM: undefined dim is reported before undefined OP_DATA when the container is false
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+function test() {
+    $x = false;
+    $x[$undefKey] = $undef;
+    var_dump($x);
+}
+test();
+?>
+--EXPECTF--
+Deprecated: Automatic conversion of false to array is deprecated in %s on line %d
+
+Warning: Undefined variable $undefKey in %s on line %d
+
+Warning: Undefined variable $undef in %s on line %d
+array(1) {
+  [""]=>
+  NULL
+}
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_007.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_007.phpt
new file mode 100644
index 00000000000..ce50f560082
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_007.phpt
@@ -0,0 +1,24 @@
+--TEST--
+JIT ASSIGN_DIM: undefined OP_DATA is not reported when the false to array deprecation throws
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+set_error_handler(function ($_, $msg) {
+    throw new Exception($msg);
+});
+function test() {
+    $x = false;
+    $x[0] = $undef;
+}
+try {
+    test();
+} catch (Exception $e) {
+    echo $e::class, ": ", $e->getMessage(), "\n";
+}
+?>
+--EXPECT--
+Exception: Automatic conversion of false to array is deprecated
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_008.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_008.phpt
new file mode 100644
index 00000000000..632ff476929
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_008.phpt
@@ -0,0 +1,27 @@
+--TEST--
+JIT ASSIGN_DIM: container converted from false is overwritten while reporting undefined OP_DATA
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+set_error_handler(function ($_, $msg) {
+    echo $msg, "\n";
+    if (str_starts_with($msg, 'Undefined variable')) {
+        $GLOBALS['x'] = 1;
+    }
+});
+function test() {
+    global $x;
+    $x = false;
+    $x[0] = $undef;
+}
+test();
+var_dump($x);
+?>
+--EXPECT--
+Automatic conversion of false to array is deprecated
+Undefined variable $undef
+int(1)
diff --git a/ext/opcache/tests/jit/assign_dim_undef_op_data_009.phpt b/ext/opcache/tests/jit/assign_dim_undef_op_data_009.phpt
new file mode 100644
index 00000000000..8a842e49162
--- /dev/null
+++ b/ext/opcache/tests/jit/assign_dim_undef_op_data_009.phpt
@@ -0,0 +1,25 @@
+--TEST--
+JIT ASSIGN_DIM: result is null when the container converted from false is copied while reporting undefined OP_DATA
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=function
+--FILE--
+<?php
+set_error_handler(function ($_, $msg) {
+    echo $msg, "\n";
+    if (str_starts_with($msg, 'Undefined variable')) {
+        $GLOBALS['copy'] = debug_backtrace()[1]['args'][0];
+    }
+});
+function test($x) {
+    $y = ($x[0] = $undef);
+    var_dump($y);
+}
+test(false);
+?>
+--EXPECT--
+Automatic conversion of false to array is deprecated
+Undefined variable $undef
+NULL