Skip to content

Commit c5f108c

Browse files
committed
Handle binary_op failure in overloaded assigns
1 parent 59d99a2 commit c5f108c

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Exception in compound assign op should prevent call to overloaded object handlers
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public function __get($k) {
8+
$this->$k = 42;
9+
return 0;
10+
}
11+
}
12+
13+
$test = new ArrayObject;
14+
$test[0] = 42;
15+
try {
16+
$test[0] %= 0;
17+
} catch (Error $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
var_dump($test);
21+
22+
$test2 = new Test;
23+
try {
24+
$test2->prop %= 0;
25+
} catch (Error $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
var_dump($test2);
29+
30+
?>
31+
--EXPECT--
32+
Modulo by zero
33+
object(ArrayObject)#1 (1) {
34+
["storage":"ArrayObject":private]=>
35+
array(1) {
36+
[0]=>
37+
int(42)
38+
}
39+
}
40+
Modulo by zero
41+
object(Test)#3 (1) {
42+
["prop"]=>
43+
int(42)
44+
}

Zend/zend_execute.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,8 +1144,9 @@ static zend_never_inline void zend_binary_assign_op_obj_dim(zval *object, zval *
11441144
}
11451145
ZVAL_COPY_VALUE(z, value);
11461146
}
1147-
binary_op(&res, Z_ISREF_P(z) ? Z_REFVAL_P(z) : z, value);
1148-
Z_OBJ_HT_P(object)->write_dimension(object, property, &res);
1147+
if (binary_op(&res, Z_ISREF_P(z) ? Z_REFVAL_P(z) : z, value) == SUCCESS) {
1148+
Z_OBJ_HT_P(object)->write_dimension(object, property, &res);
1149+
}
11491150
if (z == &rv) {
11501151
zval_ptr_dtor(&rv);
11511152
}
@@ -1538,8 +1539,9 @@ static zend_never_inline void zend_assign_op_overloaded_property(zval *object, z
15381539
}
15391540
ZVAL_COPY_VALUE(z, value);
15401541
}
1541-
binary_op(&res, z, value);
1542-
Z_OBJ_HT(obj)->write_property(&obj, property, &res, cache_slot);
1542+
if (binary_op(&res, z, value) == SUCCESS) {
1543+
Z_OBJ_HT(obj)->write_property(&obj, property, &res, cache_slot);
1544+
}
15431545
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
15441546
ZVAL_COPY(EX_VAR(opline->result.var), &res);
15451547
}

0 commit comments

Comments
 (0)