Skip to content

Commit 2337c05

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix incorrect optimization that leads to memory leak
2 parents 3e13f16 + f8f0a65 commit 2337c05

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Zend/Optimizer/sccp.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,17 @@ static int try_remove_definition(sccp_ctx *ctx, int var_num, zend_ssa_var *var,
22442244
zend_ssa_remove_result_def(ssa, ssa_op);
22452245
if (opline->opcode == ZEND_DO_ICALL) {
22462246
removed_ops = remove_call(ctx, opline, ssa_op);
2247+
} else if (opline->opcode == ZEND_TYPE_CHECK
2248+
&& (opline->op1_type & (IS_VAR|IS_TMP_VAR))
2249+
&& (!value_known(&ctx->values[ssa_op->op1_use])
2250+
|| IS_PARTIAL_ARRAY(&ctx->values[ssa_op->op1_use])
2251+
|| IS_PARTIAL_OBJECT(&ctx->values[ssa_op->op1_use]))) {
2252+
/* For TYPE_CHECK we may compute the result value without knowing the
2253+
* operand, based on type inference information. Make sure the operand is
2254+
* freed and leave further cleanup to DCE. */
2255+
opline->opcode = ZEND_FREE;
2256+
opline->result_type = IS_UNUSED;
2257+
removed_ops++;
22472258
} else {
22482259
zend_ssa_remove_instr(ssa, opline, ssa_op);
22492260
removed_ops++;

ext/opcache/tests/opt/sccp_034.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
SCCP 034: memory leak
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
--FILE--
8+
<?php
9+
is_array(["$y $y"]);
10+
?>
11+
DONE
12+
--EXPECTF--
13+
Warning: Undefined variable $y in %ssccp_034.php on line 2
14+
15+
Warning: Undefined variable $y in %ssccp_034.php on line 2
16+
DONE

0 commit comments

Comments
 (0)