[php-src] PR #25811: Fix a failed compound assignment resetting a typed property to 0

From: Date: Thu, 26 Nov 2026 22:17:41 +0000
Subject: [php-src] PR #25811: Fix a failed compound assignment resetting a typed property to 0
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-41084@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/25811 Author: ChrisAbner **The problem** When a compound assignment such as /= or += throws on a typed property, the property is set to 0. This happens in coercive typing mode, that is, without strict_types. The code can catch the exception and keep running, but the property has lost its value. ```php class Stats { public int $total = 5; } $s = new Stats; try { $s->total /= 0; } catch (DivisionByZeroError) {} var_dump($s->total); // int(0), expected int(5) ``` With an untyped property, a variable, an array element or strict_types=1, the same code keeps 5. The operator does not have to throw by itself. Laravel turns warnings into ErrorException. In a Laravel 13 app on PHP 8.4.28, $s->total += "5 apples" throws "A non-numeric value encountered" and leaves $s->total at 0. An untyped property keeps 5. **The cause** For a typed property or a typed reference, the engine computes the result in a temporary copy. Then it checks the copy against the type and writes it. zend_binary_assign_op_typed_prop() and zend_binary_assign_op_typed_ref() never check whether the operation succeeded. When it fails, the copy is left empty (UNDEF). In coercive mode the type check turns that empty value into 0 (zend_parse_arg_long_weak() treats anything below IS_TRUE as 0), and the 0 is written to the property. Untyped properties do not use a copy. The operation writes straight into the property, so the old value stays when it fails. With strict_types=1 the type check rejects the empty value, so nothing is written either. **The fix** Write the result only when zend_binary_op() returns SUCCESS. zend_binary_assign_op_obj_dim() in the same file already does this. The JIT has its own copies of these functions in zend_jit_helpers.c: zend_jit_assign_op_to_typed_prop(), zend_jit_assign_op_to_typed_ref() and zend_jit_assign_op_to_typed_ref_tmp(). They get the same check. Without that part, opcache.jit=function still leaves 0 in 7 of the 13 cases below. I also tried checking EG(exception) instead. It gave the same results in every case. I kept the return value check to match zend_binary_assign_op_obj_dim(). **Versions and branch** Reproduced on the official images of 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.28, 8.5.13 and 8.6.0. The patch applies cleanly to PHP-8.5, PHP-8.6 and master. I targeted PHP-8.4 because the value is lost silently in code that catches the exception, as in the Laravel case. 398cfb982e fixed a similar edge case of typed properties after a failed assignment, and it went into 8.0.7. If you prefer master for this one, I can retarget it. **Verification** New test Zend/tests/type_declarations/typed_properties_failed_compound_assign.phpt. It fails on 8.4.28 and passes with the fix. That holds without OPcache, with OPcache, with opcache.jit=tracing and with opcache.jit=function. Value left after the exception, on a PHP-8.4 debug build. The results are the same in all four modes. | Case | Before (8.4.28) | After | |---|---|---| | int with += [], /= 0, %= 0, <<= -1, >>= -1, -= "abc", .= new S (__toString throws) | int(0) | int(5) | | float with += [] and /= 0 | float(0) | float(2.5) | | ?int with /= 0 | int(0) | int(7) | | static int with /= 0 | int(0) | int(5) | | typed reference with /= 0 and += [] | int(0) | int(5) | | int with += "5 apples", %= 1.5, \|= 1.5 and <<= 1.5, with an error handler that throws on every warning and deprecation | int(0) | int(5) | | untyped property, variable and array element with /= 0 | 5 | 5 | Test suites with the fix, on the PHP-8.4 debug build: | Suite | Passed | Failed | |---|---|---| | Zend/tests without OPcache | 4956 | 0 | | Zend/tests with OPcache | 4960 | 0 | | ext/opcache/tests with OPcache | 948 | 12 | | ext/opcache/tests/jit with opcache.jit=tracing | 549 | 2 | The 14 failures are the inheritance cache, path, phar and preload restart tests (bug66338, bug78106, gh17733, gh20469 ×3, gh24534, gh8739, gh8846, issue0115, issue0149, revalidate_path_01, preload_restart_function_jit, preload_restart_hot_counters). They fail the same way on the base commit without the fix in my environment. On an ASan build of the same commit, the reproducers gave no reports in any of the four modes. Zend/tests/type_declarations and ext/opcache/tests/jit/assign_op_*.phpt with --asan passed too (465 tests, 0 failures). **Not covered** No PHP code I tried reaches zend_jit_assign_op_to_typed_ref_tmp(). With a temporary operand, the JIT falls back to the VM handler. The change there is the same line as in the other two helpers. With opcache.jit=tracing, every form I tried also goes through the VM handler. So in tracing mode the fix in zend_execute.c is the part that applies.

« previous php.git-pulls (#41084) next »