Skip to content

Commit d877d18

Browse files
committed
Error on by-ref assign to overloaded prop returning ref
This error was already thrown if __get() was used -- however not if it returned by reference. This is incorrect, because the reference return makes no difference to a by-reference assignment, which has reference-breaking semantics. The result was that the assignment was accepted silently, even though it didn't do anything (not even the value was assigned, let alone the reference).
1 parent eed3637 commit d877d18

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Cannot assign by reference to overloaded object, even if __get() returns by-ref
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
private $x;
8+
public function &__get($name) {
9+
return $this->x;
10+
}
11+
}
12+
13+
$test = new Test;
14+
$y = 5;
15+
$test->x =& $y;
16+
var_dump($test->x);
17+
18+
?>
19+
--EXPECTF--
20+
Fatal error: Uncaught Error: Cannot assign by reference to overloaded object in %s:%d
21+
Stack trace:
22+
#0 {main}
23+
thrown in %s on line %d

Zend/zend_vm_def.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,6 @@ ZEND_VM_HANDLER(39, ZEND_ASSIGN_REF, VAR|CV, VAR|CV, SRC)
23252325

23262326
if (OP1_TYPE == IS_VAR &&
23272327
UNEXPECTED(Z_TYPE_P(EX_VAR(opline->op1.var)) != IS_INDIRECT) &&
2328-
UNEXPECTED(!Z_ISREF_P(EX_VAR(opline->op1.var))) &&
23292328
UNEXPECTED(!Z_ISERROR_P(EX_VAR(opline->op1.var)))) {
23302329

23312330
zend_throw_error(NULL, "Cannot assign by reference to overloaded object");

Zend/zend_vm_execute.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26374,7 +26374,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_REF_SPEC_VAR_VAR_HANDLE
2637426374

2637526375
if (IS_VAR == IS_VAR &&
2637626376
UNEXPECTED(Z_TYPE_P(EX_VAR(opline->op1.var)) != IS_INDIRECT) &&
26377-
UNEXPECTED(!Z_ISREF_P(EX_VAR(opline->op1.var))) &&
2637826377
UNEXPECTED(!Z_ISERROR_P(EX_VAR(opline->op1.var)))) {
2637926378

2638026379
zend_throw_error(NULL, "Cannot assign by reference to overloaded object");
@@ -29428,7 +29427,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_REF_SPEC_VAR_CV_HANDLER
2942829427

2942929428
if (IS_VAR == IS_VAR &&
2943029429
UNEXPECTED(Z_TYPE_P(EX_VAR(opline->op1.var)) != IS_INDIRECT) &&
29431-
UNEXPECTED(!Z_ISREF_P(EX_VAR(opline->op1.var))) &&
2943229430
UNEXPECTED(!Z_ISERROR_P(EX_VAR(opline->op1.var)))) {
2943329431

2943429432
zend_throw_error(NULL, "Cannot assign by reference to overloaded object");
@@ -45059,7 +45057,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_REF_SPEC_CV_VAR_HANDLER
4505945057

4506045058
if (IS_CV == IS_VAR &&
4506145059
UNEXPECTED(Z_TYPE_P(EX_VAR(opline->op1.var)) != IS_INDIRECT) &&
45062-
UNEXPECTED(!Z_ISREF_P(EX_VAR(opline->op1.var))) &&
4506345060
UNEXPECTED(!Z_ISERROR_P(EX_VAR(opline->op1.var)))) {
4506445061

4506545062
zend_throw_error(NULL, "Cannot assign by reference to overloaded object");
@@ -49572,7 +49569,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_REF_SPEC_CV_CV_HANDLER(
4957249569

4957349570
if (IS_CV == IS_VAR &&
4957449571
UNEXPECTED(Z_TYPE_P(EX_VAR(opline->op1.var)) != IS_INDIRECT) &&
49575-
UNEXPECTED(!Z_ISREF_P(EX_VAR(opline->op1.var))) &&
4957649572
UNEXPECTED(!Z_ISERROR_P(EX_VAR(opline->op1.var)))) {
4957749573

4957849574
zend_throw_error(NULL, "Cannot assign by reference to overloaded object");

0 commit comments

Comments
 (0)