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
39 changes: 39 additions & 0 deletions Zend/tests/gh10497.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-10497: Allow direct modification of object stored in a constant
--FILE--
<?php
const a = new stdClass;
a->b = 42;
var_dump(a->b);

const obj = new stdClass;
obj->foo = 'bar';
obj->baz = 123;
var_dump(obj->foo, obj->baz);

const nested = new stdClass;
nested->inner = new stdClass;
nested->inner->value = 999;
var_dump(nested->inner->value);

const readTest = new stdClass;
readTest->prop = 'test';
echo readTest->prop . "\n";

var_dump(isset(readTest->prop));
var_dump(empty(readTest->missing));

const modTest = new stdClass;
modTest->val = 1;
modTest->val = 2;
var_dump(modTest->val);
?>
--EXPECT--
int(42)
string(3) "bar"
int(123)
int(999)
test
bool(true)
bool(true)
int(2)
14 changes: 8 additions & 6 deletions Zend/tests/gh12102_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ GH-12102: Incorrect "Cannot use temporary expression in write context" error for

function test() {
byVal(C[0]);
try {
byRef(C[0]);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
byRef(C[0]);
var_dump(C);
}

/* Intentionally declared after test() to avoid compile-time checking of ref args. */
Expand All @@ -21,6 +18,7 @@ function byVal($arg) {
}

function byRef(&$arg) {
$arg = 'modified';
var_dump($arg);
}

Expand All @@ -29,4 +27,8 @@ test('y');
?>
--EXPECT--
string(3) "foo"
Cannot use temporary expression in write context
string(8) "modified"
array(1) {
[0]=>
string(3) "foo"
}
21 changes: 17 additions & 4 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -11101,7 +11101,7 @@ static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
}
/* }}} */

static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
static zend_op *zend_compile_const_inner(znode *result, const zend_ast *ast, bool use_tmp) /* {{{ */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zend_compile_const() is only used from one other place, no need for this BC layer. You can just pass BP_VAR_R from zend_compile_expr_inner().

{
zend_ast *name_ast = ast->child[0];

Expand All @@ -11125,17 +11125,21 @@ static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
result->op_type = IS_CONST;
ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
zend_string_release_ex(resolved_name, 0);
return;
return NULL;
}
}

if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
result->op_type = IS_CONST;
zend_string_release_ex(resolved_name, 0);
return;
return NULL;
}

opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
if (use_tmp) {
opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
} else {
opline = zend_emit_op(result, ZEND_FETCH_CONSTANT, NULL, NULL);
}
opline->op2_type = IS_CONST;

if (is_fully_qualified || !FC(current_namespace)) {
Expand All @@ -11148,6 +11152,13 @@ static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
resolved_name, true);
}
opline->extended_value = zend_alloc_cache_slot();
return opline;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return value should be unneded. It's only needed for specific opcodes, e.g. to add some extended_value flag. Just return NULL from zend_compile_var_inner().

}
/* }}} */

static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
{
zend_compile_const_inner(result, ast, true);
}
/* }}} */

Expand Down Expand Up @@ -12085,6 +12096,8 @@ static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t ty
case ZEND_AST_ZNODE:
*result = *zend_ast_get_znode(ast);
return NULL;
case ZEND_AST_CONST:
return zend_compile_const_inner(result, ast, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pass type directly and only produce IS_VAR when in write-context (BP_VAR_W or BP_VAR_RW).

default:
if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
zend_error_noreturn(E_COMPILE_ERROR,
Expand Down
Loading