[php-src] PHP-8.5: Fix GH-23644: crash on a constant-vs-constant empty array comparison

From: Date: Sat, 12 Sep 2026 10:41:25 +0000
Subject: [php-src] PHP-8.5: Fix GH-23644: crash on a constant-vs-constant empty array comparison
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141177@lists.php.net to get a copy of this message
Author: Lazizbek Ergashev (lazerg) Committer: ndossche (ndossche) Date: 2026-09-12T12:40:23+02:00 Commit: https://github.com/php/php-src/commit/7c94b351dbb44f51cf210540f49cddae64fe2037 Raw diff: https://github.com/php/php-src/commit/7c94b351dbb44f51cf210540f49cddae64fe2037.diff Fix GH-23644: crash on a constant-vs-constant empty array comparison Co-authored-by: ndossche <7771979+ndossche@users.noreply.github.com> Changed paths: A ext/opcache/tests/opt/gh23644.phpt M NEWS M Zend/zend_vm_def.h M Zend/zend_vm_execute.h Diff: diff --git a/NEWS b/NEWS index 8c4131fd6426..47e627b104b2 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,8 @@ PHP NEWS - Core: . Fixed incorrect internal pointer and foreach iterator positions when compacting arrays with holes. (Weilin Du) + . Fixed bug GH-23644 (Optimizer leaves a constant-vs-constant comparison + unfolded, crashing the VM in zval_undefined_cv). (ndossche) - DOM: . Fixed use-after-free when re-constructing a DOMXPath whose php:function diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 90a666ab71e7..27b0e44218fd 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -10105,7 +10105,7 @@ ZEND_VM_HOT_TYPE_SPEC_HANDLER(ZEND_IS_NOT_EQUAL|ZEND_IS_NOT_IDENTICAL, (op1_info ZEND_VM_SMART_BRANCH(result, 0); } -ZEND_VM_TYPE_SPEC_HANDLER(ZEND_IS_IDENTICAL, op->op2_type == IS_CONST && (Z_TYPE_P(RT_CONSTANT(op, op->op2)) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(RT_CONSTANT(op, op->op2))) == 0), ZEND_IS_IDENTICAL_EMPTY_ARRAY, TMPVARCV, CONST, SPEC(SMART_BRANCH,COMMUTATIVE)) +ZEND_VM_TYPE_SPEC_HANDLER(ZEND_IS_IDENTICAL, op->op2_type == IS_CONST && (Z_TYPE_P(RT_CONSTANT(op, op->op2)) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(RT_CONSTANT(op, op->op2))) == 0), ZEND_IS_IDENTICAL_EMPTY_ARRAY, TMPVARCV, CONST, SPEC(SMART_BRANCH,NO_CONST_CONST,COMMUTATIVE)) { USE_OPLINE zval *op1; @@ -10118,7 +10118,7 @@ ZEND_VM_TYPE_SPEC_HANDLER(ZEND_IS_IDENTICAL, op->op2_type == IS_CONST && (Z_TYPE ZEND_VM_SMART_BRANCH(result, 0); } -ZEND_VM_TYPE_SPEC_HANDLER(ZEND_IS_NOT_IDENTICAL, op->op2_type == IS_CONST && (Z_TYPE_P(RT_CONSTANT(op, op->op2)) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(RT_CONSTANT(op, op->op2))) == 0), ZEND_IS_NOT_IDENTICAL_EMPTY_ARRAY, TMPVARCV, CONST, SPEC(SMART_BRANCH,COMMUTATIVE)) +ZEND_VM_TYPE_SPEC_HANDLER(ZEND_IS_NOT_IDENTICAL, op->op2_type == IS_CONST && (Z_TYPE_P(RT_CONSTANT(op, op->op2)) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(RT_CONSTANT(op, op->op2))) == 0), ZEND_IS_NOT_IDENTICAL_EMPTY_ARRAY, TMPVARCV, CONST, SPEC(SMART_BRANCH,NO_CONST_CONST,COMMUTATIVE)) { USE_OPLINE zval *op1; diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 9a98804f25c2..2ba4011cf2b0 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -129502,6 +129502,9 @@ ZEND_API void ZEND_FASTCALL zend_vm_set_opcode_handler_ex(zend_op* op, uint32_t } spec = 2886 | SPEC_RULE_OP1 | SPEC_RULE_OP2 | SPEC_RULE_SMART_BRANCH | SPEC_RULE_COMMUTATIVE; } else if (op->op2_type == IS_CONST && (Z_TYPE_P(RT_CONSTANT(op, op->op2)) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(RT_CONSTANT(op, op->op2))) == 0)) { + if (op->op1_type == IS_CONST && op->op2_type == IS_CONST) { + break; + } spec = 3111 | SPEC_RULE_SMART_BRANCH | SPEC_RULE_COMMUTATIVE; } else if (op->op1_type == IS_CV && (op->op2_type & (IS_CONST|IS_CV)) && !(op1_info & (MAY_BE_UNDEF|MAY_BE_REF)) && !(op2_info & (MAY_BE_UNDEF|MAY_BE_REF))) { spec = 3117 | SPEC_RULE_OP2 | SPEC_RULE_COMMUTATIVE; @@ -129522,6 +129525,9 @@ ZEND_API void ZEND_FASTCALL zend_vm_set_opcode_handler_ex(zend_op* op, uint32_t } spec = 3036 | SPEC_RULE_OP1 | SPEC_RULE_OP2 | SPEC_RULE_SMART_BRANCH | SPEC_RULE_COMMUTATIVE; } else if (op->op2_type == IS_CONST && (Z_TYPE_P(RT_CONSTANT(op, op->op2)) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(RT_CONSTANT(op, op->op2))) == 0)) { + if (op->op1_type == IS_CONST && op->op2_type == IS_CONST) { + break; + } spec = 3114 | SPEC_RULE_SMART_BRANCH | SPEC_RULE_COMMUTATIVE; } else if (op->op1_type == IS_CV && (op->op2_type & (IS_CONST|IS_CV)) && !(op1_info & (MAY_BE_UNDEF|MAY_BE_REF)) && !(op2_info & (MAY_BE_UNDEF|MAY_BE_REF))) { spec = 3122 | SPEC_RULE_OP2 | SPEC_RULE_COMMUTATIVE; diff --git a/ext/opcache/tests/opt/gh23644.phpt b/ext/opcache/tests/opt/gh23644.phpt new file mode 100644 index 000000000000..3094722c32a6 --- /dev/null +++ b/ext/opcache/tests/opt/gh23644.phpt @@ -0,0 +1,28 @@ +--TEST-- +GH-23644 (Crash on a constant-vs-constant empty array comparison) +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.optimization_level=-1 +--EXTENSIONS-- +opcache +--CREDITS-- +LTSCommerce +--FILE-- +<?php +function f($x) { + if (null !== $x || [] !== $x) { return 1; } + return 2; +} + +function g($x) { + if ($x === null) { return $x === [] ? 'eq' : 'ne'; } + return 'other'; +} + +var_dump(f(null)); +var_dump(g(null)); +?> +--EXPECT-- +int(1) +string(2) "ne"

« previous php.cvs (#141177) next »