Skip to content

Commit c64f5e3

Browse files
committed
Disallow direct incdec of function return value
Matching PHP 5 behavior. We may want to support this for by-reference returns, but that requires implementing further checks.
1 parent f678519 commit c64f5e3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
It's not possible to increment the return value of a function
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
return 42;
8+
}
9+
10+
++test();
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Can't use function return value in write context in %s on line %d

Zend/zend_compile.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,7 @@ static void zend_compile_list_assign(znode *result, zend_ast *ast, znode *expr_n
23432343
}
23442344
/* }}} */
23452345

2346-
void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
2346+
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
23472347
{
23482348
if (ast->kind == ZEND_AST_CALL) {
23492349
zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
@@ -5648,6 +5648,8 @@ void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
56485648
zend_ast *var_ast = ast->child[0];
56495649
ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
56505650

5651+
zend_ensure_writable_variable(var_ast);
5652+
56515653
if (var_ast->kind == ZEND_AST_PROP) {
56525654
zend_op *opline = zend_compile_prop_common(NULL, var_ast, BP_VAR_RW);
56535655
opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
@@ -5666,6 +5668,8 @@ void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
56665668
zend_ast *var_ast = ast->child[0];
56675669
ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
56685670

5671+
zend_ensure_writable_variable(var_ast);
5672+
56695673
if (var_ast->kind == ZEND_AST_PROP) {
56705674
zend_op *opline = zend_compile_prop_common(result, var_ast, BP_VAR_RW);
56715675
opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;

0 commit comments

Comments
 (0)