Skip to content

Commit 748adf1

Browse files
committed
Fix zend_separate_if_call_and_write for FUNC_ARGs
Fixes GH-12102 Closees GH-12140
1 parent a648d39 commit 748adf1

File tree

6 files changed

+133
-3
lines changed

6 files changed

+133
-3
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010
closures). (ilutov)
1111
. Fixed bug GH-12060 (Internal iterator rewind handler is called twice).
1212
(ju1ius)
13+
. Fixed bug GH-12102 (Incorrect compile error when using array access on TMP
14+
value in function call). (ilutov)
1315

1416
- DOM:
1517
. Fix memory leak when setting an invalid DOMDocument encoding. (nielsdos)

Zend/Optimizer/optimize_func_calls.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
typedef struct _optimizer_call_info {
3939
zend_function *func;
4040
zend_op *opline;
41+
zend_op *last_check_func_arg_opline;
4142
bool is_prototype;
4243
bool try_inline;
4344
uint32_t func_arg_num;
@@ -252,6 +253,14 @@ void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx)
252253
if (call_stack[call - 1].func_arg_num != (uint32_t)-1
253254
&& has_known_send_mode(&call_stack[call - 1], call_stack[call - 1].func_arg_num)) {
254255
if (ARG_SHOULD_BE_SENT_BY_REF(call_stack[call - 1].func, call_stack[call - 1].func_arg_num)) {
256+
/* There's no TMP specialization for FETCH_OBJ_W/FETCH_DIM_W. Avoid
257+
* converting it and error at runtime in the FUNC_ARG variant. */
258+
if ((opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG)
259+
&& (opline->op1_type == IS_TMP_VAR || call_stack[call - 1].last_check_func_arg_opline == NULL)) {
260+
/* Don't remove the associated CHECK_FUNC_ARG opcode. */
261+
call_stack[call - 1].last_check_func_arg_opline = NULL;
262+
break;
263+
}
255264
if (opline->opcode != ZEND_FETCH_STATIC_PROP_FUNC_ARG) {
256265
opline->opcode -= 9;
257266
} else {
@@ -298,11 +307,21 @@ void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx)
298307

299308
if (has_known_send_mode(&call_stack[call - 1], opline->op2.num)) {
300309
call_stack[call - 1].func_arg_num = opline->op2.num;
301-
MAKE_NOP(opline);
310+
call_stack[call - 1].last_check_func_arg_opline = opline;
302311
}
303312
break;
304-
case ZEND_SEND_VAR_EX:
305313
case ZEND_SEND_FUNC_ARG:
314+
/* Don't transform SEND_FUNC_ARG if any FETCH opcodes weren't transformed. */
315+
if (call_stack[call - 1].last_check_func_arg_opline == NULL) {
316+
if (opline->op2_type == IS_CONST) {
317+
call_stack[call - 1].try_inline = 0;
318+
}
319+
break;
320+
}
321+
MAKE_NOP(call_stack[call - 1].last_check_func_arg_opline);
322+
call_stack[call - 1].last_check_func_arg_opline = NULL;
323+
ZEND_FALLTHROUGH;
324+
case ZEND_SEND_VAR_EX:
306325
if (opline->op2_type == IS_CONST) {
307326
call_stack[call - 1].try_inline = 0;
308327
break;

Zend/tests/gh12102_1.phpt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-12102: Incorrect "Cannot use temporary expression in write context" error for BP_VAR_FUNC_ARG
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
byVal(func_get_args()[0]);
8+
try {
9+
byRef(func_get_args()[0]);
10+
} catch (Error $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
}
14+
15+
/* Intentionally declared after test() to avoid compile-time checking of ref args. */
16+
17+
function byVal($arg) {
18+
var_dump($arg);
19+
}
20+
21+
function byRef(&$arg) {
22+
var_dump($arg);
23+
}
24+
25+
test('y');
26+
27+
?>
28+
--EXPECT--
29+
string(1) "y"
30+
Cannot use temporary expression in write context

Zend/tests/gh12102_2.phpt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
GH-12102: Incorrect "Cannot use temporary expression in write context" error for BP_VAR_FUNC_ARG
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
global $ref;
8+
byVal(getRef()[0]);
9+
var_dump($ref);
10+
byRef(getRef()[0]);
11+
var_dump($ref);
12+
}
13+
14+
/* Intentionally declared after test() to avoid compile-time checking of ref args. */
15+
16+
function &getRef() {
17+
global $ref;
18+
$ref = [];
19+
return $ref;
20+
}
21+
22+
function byVal($arg) {
23+
$arg[] = 42;
24+
}
25+
26+
function byRef(&$arg) {
27+
$arg[] = 42;
28+
}
29+
30+
test();
31+
32+
?>
33+
--EXPECTF--
34+
Warning: Undefined array key 0 in %s on line %d
35+
array(0) {
36+
}
37+
array(1) {
38+
[0]=>
39+
array(1) {
40+
[0]=>
41+
int(42)
42+
}
43+
}

Zend/tests/gh12102_3.phpt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
GH-12102: Incorrect "Cannot use temporary expression in write context" error for BP_VAR_FUNC_ARG
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
byVal(C[0]);
8+
try {
9+
byRef(C[0]);
10+
} catch (Error $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
}
14+
15+
/* Intentionally declared after test() to avoid compile-time checking of ref args. */
16+
17+
const C = ['foo'];
18+
19+
function byVal($arg) {
20+
var_dump($arg);
21+
}
22+
23+
function byRef(&$arg) {
24+
var_dump($arg);
25+
}
26+
27+
test('y');
28+
29+
?>
30+
--EXPECT--
31+
string(3) "foo"
32+
Cannot use temporary expression in write context

Zend/zend_compile.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -2816,7 +2816,11 @@ static zend_op *zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t t
28162816

28172817
static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
28182818
{
2819-
if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
2819+
if (type != BP_VAR_R
2820+
&& type != BP_VAR_IS
2821+
/* Whether a FUNC_ARG is R may only be determined at runtime. */
2822+
&& type != BP_VAR_FUNC_ARG
2823+
&& zend_is_call(ast)) {
28202824
if (node->op_type == IS_VAR) {
28212825
zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
28222826
opline->result_type = IS_VAR;

0 commit comments

Comments
 (0)