Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
--TEST--
A compound assignment that throws must leave a typed property or reference unchanged
--FILE--
<?php

class C {
public int $i = 5;
public float $f = 2.5;
public ?int $n = 7;
public static int $s = 5;
}

class S {
public function __toString(): string { throw new Exception('toString'); }
}

function test(string $label, Closure $op, Closure $get) {
try {
$op();
echo "$label: no exception\n";
} catch (Throwable $e) {
echo "$label: ", get_class($e), "\n";
}
var_dump($get());
}

$o = new C;
test('int += []', function () use ($o) { $o->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)
8 changes: 4 additions & 4 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions ext/opcache/jit/zend_jit_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
Loading