Skip to content

Commit e184f86

Browse files
committed
Fix zend_separate_if_call_and_write for FUNC_ARGs
Fixes GH-12102
1 parent a648d39 commit e184f86

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

Zend/Optimizer/optimize_func_calls.c

Lines changed: 10 additions & 1 deletion
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 *check_func_arg_opline;
4142
bool is_prototype;
4243
bool try_inline;
4344
uint32_t func_arg_num;
@@ -252,6 +253,13 @@ 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_DIM_W. Avoid converting it and
257+
* error at runtime in the FUNC_ARG variant. */
258+
if (opline->opcode == ZEND_FETCH_DIM_FUNC_ARG && opline->op1_type == IS_TMP_VAR) {
259+
/* Don't remove the associated CHECK_FUNC_ARG opcode. */
260+
call_stack[call - 1].check_func_arg_opline = NULL;
261+
break;
262+
}
255263
if (opline->opcode != ZEND_FETCH_STATIC_PROP_FUNC_ARG) {
256264
opline->opcode -= 9;
257265
} else {
@@ -272,6 +280,7 @@ void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx)
272280
opline->opcode = ZEND_FETCH_STATIC_PROP_R;
273281
}
274282
}
283+
MAKE_NOP(call_stack[call - 1].check_func_arg_opline);
275284
}
276285
break;
277286
case ZEND_SEND_VAL_EX:
@@ -298,7 +307,7 @@ 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].check_func_arg_opline = opline;
302311
}
303312
break;
304313
case ZEND_SEND_VAR_EX:

Zend/tests/gh12102_1.phpt

Lines changed: 30 additions & 0 deletions
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+
variadicByVal(func_get_args()[0]);
8+
try {
9+
variadicByRef(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 variadicByVal($arg) {
18+
var_dump($arg);
19+
}
20+
21+
function variadicByRef(&$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

Lines changed: 43 additions & 0 deletions
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+
variadicByVal(getRef()[0]);
9+
var_dump($ref);
10+
variadicByRef(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 variadicByVal($arg) {
23+
$arg[] = 42;
24+
}
25+
26+
function variadicByRef(&$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/zend_compile.c

Lines changed: 5 additions & 1 deletion
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)