Skip to content

Commit ce74ff2

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Fixed bug #80046
2 parents 67e9b3b + 04e77d2 commit ce74ff2

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Zend/tests/bug80046.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #80046: FREE for SWITCH_STRING optimized away
3+
--FILE--
4+
<?php
5+
6+
function test($foo) {
7+
switch ($foo . 'Bar') {
8+
case 'A':
9+
throw new Exception('A');
10+
default:
11+
throw new Exception('Default');
12+
}
13+
}
14+
try {
15+
test('Foo');
16+
} catch (Exception $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
20+
?>
21+
--EXPECT--
22+
Default

ext/opcache/Optimizer/block_pass.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,15 @@ static void assemble_code_blocks(zend_cfg *cfg, zend_op_array *op_array, zend_op
967967
if (b->len == 0) {
968968
continue;
969969
}
970-
if (b->flags & ZEND_BB_REACHABLE) {
970+
if (b->flags & (ZEND_BB_REACHABLE|ZEND_BB_UNREACHABLE_FREE)) {
971+
if (b->flags & ZEND_BB_UNREACHABLE_FREE) {
972+
/* Only keep the FREE for the loop var */
973+
ZEND_ASSERT(op_array->opcodes[b->start].opcode == ZEND_FREE
974+
|| op_array->opcodes[b->start].opcode == ZEND_FE_FREE);
975+
len += b->len = 1;
976+
continue;
977+
}
978+
971979
opline = op_array->opcodes + b->start + b->len - 1;
972980
if (opline->opcode == ZEND_JMP) {
973981
zend_basic_block *next = b + 1;
@@ -1005,7 +1013,7 @@ static void assemble_code_blocks(zend_cfg *cfg, zend_op_array *op_array, zend_op
10051013

10061014
/* Copy code of reachable blocks into a single buffer */
10071015
for (b = blocks; b < end; b++) {
1008-
if (b->flags & ZEND_BB_REACHABLE) {
1016+
if (b->flags & (ZEND_BB_REACHABLE|ZEND_BB_UNREACHABLE_FREE)) {
10091017
memcpy(opline, op_array->opcodes + b->start, b->len * sizeof(zend_op));
10101018
b->start = opline - new_opcodes;
10111019
opline += b->len;
@@ -1131,7 +1139,7 @@ static void assemble_code_blocks(zend_cfg *cfg, zend_op_array *op_array, zend_op
11311139
/* rebuild map (just for printing) */
11321140
memset(cfg->map, -1, sizeof(int) * op_array->last);
11331141
for (n = 0; n < cfg->blocks_count; n++) {
1134-
if (cfg->blocks[n].flags & ZEND_BB_REACHABLE) {
1142+
if (cfg->blocks[n].flags & (ZEND_BB_REACHABLE|ZEND_BB_UNREACHABLE_FREE)) {
11351143
cfg->map[cfg->blocks[n].start] = n;
11361144
}
11371145
}

ext/opcache/Optimizer/zend_cfg.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,8 @@ int zend_build_cfg(zend_arena **arena, const zend_op_array *op_array, uint32_t b
593593
}
594594

595595
/* Build CFG, Step 4, Mark Reachable Basic Blocks */
596-
zend_mark_reachable_blocks(op_array, cfg, 0);
597-
598596
cfg->flags |= flags;
597+
zend_mark_reachable_blocks(op_array, cfg, 0);
599598

600599
return SUCCESS;
601600
}

0 commit comments

Comments
 (0)