From a4b0b16a095c642307af2251750b71dd49bc80cf Mon Sep 17 00:00:00 2001 From: ChrisAbner Date: Wed, 25 Nov 2026 22:41:59 -0500 Subject: [PATCH] Fix a failed compound assignment resetting a typed property to 0 The typed property and typed reference versions of a compound assignment compute the result into a separate zval and then verify it against the type, without checking whether the operation failed. When it throws, for example on $o->i /= 0 or $o->i += [], the result is UNDEF. In coercive typing mode the type check turns it into 0 and assigns it, so the property loses its value even though the exception is caught. Untyped properties and plain variables keep their value, because the result is written over the operand. Only assign the result when the binary operation succeeds, as zend_binary_assign_op_obj_dim() already does. The JIT has its own copies of these helpers, so they get the same check. --- ...ped_properties_failed_compound_assign.phpt | 80 +++++++++++++++++++ Zend/zend_execute.c | 8 +- ext/opcache/jit/zend_jit_helpers.c | 12 +-- 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 Zend/tests/type_declarations/typed_properties_failed_compound_assign.phpt diff --git a/Zend/tests/type_declarations/typed_properties_failed_compound_assign.phpt b/Zend/tests/type_declarations/typed_properties_failed_compound_assign.phpt new file mode 100644 index 000000000000..213b7ab4a7dc --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_failed_compound_assign.phpt @@ -0,0 +1,80 @@ +--TEST-- +A compound assignment that throws must leave a typed property or reference unchanged +--FILE-- +i += []; }, fn () => $o->i); +test('int /= 0', function () use ($o) { $o->i /= 0; }, fn () => $o->i); +test('int %= 0', function () use ($o) { $o->i %= 0; }, fn () => $o->i); +test('int <<= -1', function () use ($o) { $o->i <<= -1; }, fn () => $o->i); +test('int -= "abc"', function () use ($o) { $o->i -= "abc"; }, fn () => $o->i); +test('int .= S', function () use ($o) { $o->i .= new S; }, fn () => $o->i); +test('float += []', function () use ($o) { $o->f += []; }, fn () => $o->f); +test('?int /= 0', function () use ($o) { $o->n /= 0; }, fn () => $o->n); +test('static int /= 0', function () { C::$s /= 0; }, fn () => C::$s); + +$r = &$o->i; +test('typed ref /= 0', function () use (&$r) { $r /= 0; }, fn () => $o->i); +test('typed ref += []', function () use (&$r) { $r += []; }, fn () => $o->i); +unset($r); + +test('int += 1', function () use ($o) { $o->i += 1; }, fn () => $o->i); + +set_error_handler(function ($no, $msg) { throw new ErrorException($msg, 0, $no); }); +$o = new C; +test('int += "5 apples" with a throwing error handler', function () use ($o) { $o->i += "5 apples"; }, fn () => $o->i); +test('int %= 1.5 with a throwing error handler', function () use ($o) { $o->i %= 1.5; }, fn () => $o->i); +restore_error_handler(); + +?> +--EXPECT-- +int += []: TypeError +int(5) +int /= 0: DivisionByZeroError +int(5) +int %= 0: DivisionByZeroError +int(5) +int <<= -1: ArithmeticError +int(5) +int -= "abc": TypeError +int(5) +int .= S: Exception +int(5) +float += []: TypeError +float(2.5) +?int /= 0: DivisionByZeroError +int(7) +static int /= 0: DivisionByZeroError +int(5) +typed ref /= 0: DivisionByZeroError +int(5) +typed ref += []: TypeError +int(5) +int += 1: no exception +int(6) +int += "5 apples" with a throwing error handler: ErrorException +int(5) +int %= 1.5 with a throwing error handler: ErrorException +int(5) diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index e49a6b120c26..8ab146d6d7ee 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -1698,8 +1698,8 @@ static zend_never_inline void zend_binary_assign_op_typed_ref(zend_reference *re return; } - zend_binary_op(&z_copy, &ref->val, value OPLINE_CC); - if (EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, EX_USES_STRICT_TYPES()))) { + if (EXPECTED(zend_binary_op(&z_copy, &ref->val, value OPLINE_CC) == SUCCESS) + && EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, EX_USES_STRICT_TYPES()))) { zval_ptr_dtor(&ref->val); ZVAL_COPY_VALUE(&ref->val, &z_copy); } else { @@ -1718,8 +1718,8 @@ static zend_never_inline void zend_binary_assign_op_typed_prop(zend_property_inf return; } - zend_binary_op(&z_copy, zptr, value OPLINE_CC); - if (EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) { + if (EXPECTED(zend_binary_op(&z_copy, zptr, value OPLINE_CC) == SUCCESS) + && EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) { zval_ptr_dtor(zptr); ZVAL_COPY_VALUE(zptr, &z_copy); } else { diff --git a/ext/opcache/jit/zend_jit_helpers.c b/ext/opcache/jit/zend_jit_helpers.c index acf25d05e3bf..53915dd78db7 100644 --- a/ext/opcache/jit/zend_jit_helpers.c +++ b/ext/opcache/jit/zend_jit_helpers.c @@ -2492,8 +2492,8 @@ static void ZEND_FASTCALL zend_jit_assign_op_to_typed_ref(zend_reference *ref, z return; } - binary_op(&z_copy, &ref->val, val); - if (EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))))) { + if (EXPECTED(binary_op(&z_copy, &ref->val, val) == SUCCESS) + && EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))))) { zval_ptr_dtor(&ref->val); ZVAL_COPY_VALUE(&ref->val, &z_copy); } else { @@ -2505,8 +2505,8 @@ static void ZEND_FASTCALL zend_jit_assign_op_to_typed_ref_tmp(zend_reference *re { zval z_copy; - binary_op(&z_copy, &ref->val, val); - if (EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))))) { + if (EXPECTED(binary_op(&z_copy, &ref->val, val) == SUCCESS) + && EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))))) { zval_ptr_dtor(&ref->val); ZVAL_COPY_VALUE(&ref->val, &z_copy); } else { @@ -2772,8 +2772,8 @@ static void ZEND_FASTCALL zend_jit_assign_op_to_typed_prop(zval *zptr, zend_prop return; } - binary_op(&z_copy, zptr, value); - if (EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) { + if (EXPECTED(binary_op(&z_copy, zptr, value) == SUCCESS) + && EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) { Z_PROP_FLAG_P(zptr) &= ~IS_PROP_REINITABLE; zval_ptr_dtor(zptr); ZVAL_COPY_VALUE(zptr, &z_copy);