Skip to content

Commit f491dab

Browse files
committed
Fix nullsafe operator on reference
Dereference the value before checking the type. As the happy path necessarily has to check for references, I'm not bothering to delay the comparison.
1 parent 75baa72 commit f491dab

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Zend/tests/nullsafe_operator/031.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Nullsafe operator on referenced value
3+
--FILE--
4+
<?php
5+
6+
$val = null;
7+
$ref =& $val;
8+
var_dump($ref?->foo);
9+
10+
$val = new stdClass;
11+
var_dump($ref?->foo);
12+
13+
?>
14+
--EXPECTF--
15+
NULL
16+
17+
Warning: Undefined property: stdClass::$foo in %s on line %d
18+
NULL

Zend/zend_vm_def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7325,6 +7325,9 @@ ZEND_VM_HOT_NOCONST_HANDLER(198, ZEND_JMP_NULL, CONST|TMPVARCV, JMP_ADDR)
73257325
zval *val;
73267326

73277327
val = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
7328+
if (OP1_TYPE != IS_CONST) {
7329+
ZVAL_DEREF(val);
7330+
}
73287331

73297332
if (Z_TYPE_INFO_P(val) > IS_NULL) {
73307333
ZEND_VM_NEXT_OPCODE();

Zend/zend_vm_execute.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,6 +4375,9 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_JMP_NULL_SPEC_CON
43754375
zval *val;
43764376

43774377
val = RT_CONSTANT(opline, opline->op1);
4378+
if (IS_CONST != IS_CONST) {
4379+
ZVAL_DEREF(val);
4380+
}
43784381

43794382
if (Z_TYPE_INFO_P(val) > IS_NULL) {
43804383
ZEND_VM_NEXT_OPCODE();
@@ -11095,6 +11098,9 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_JMP_NULL_SPEC_TMPV
1109511098
zval *val;
1109611099

1109711100
val = EX_VAR(opline->op1.var);
11101+
if ((IS_TMP_VAR|IS_VAR|IS_CV) != IS_CONST) {
11102+
ZVAL_DEREF(val);
11103+
}
1109811104

1109911105
if (Z_TYPE_INFO_P(val) > IS_NULL) {
1110011106
ZEND_VM_NEXT_OPCODE();

0 commit comments

Comments
 (0)