Skip to content

Commit 178bbe3

Browse files
committed
Fixed bug #81015
Make sure that the previous opline is part of the same block, otherwise it may be non-dominating. The test case does not fail on PHP-7.4, but I think the general problem can appear on 7.4 as well, so I'm applying the patch to that branch.
1 parent 896e4d3 commit 178bbe3

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ PHP NEWS
3434

3535
- Opcache:
3636
. Fixed bug #80900 (switch statement behavior inside function). (twosee)
37+
. Fixed bug #81015 (Opcache optimization assumes wrong part of ternary
38+
operator in if-condition). (Nikita)
3739

3840
- XMLReader:
3941
. Fixed bug #73246 (XMLReader: encoding length not checked). (cmb)

ext/opcache/Optimizer/zend_ssa.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ static void place_essa_pis(
255255
default:
256256
continue;
257257
}
258+
259+
/* The following patterns all inspect the opline directly before the JMPZ opcode.
260+
* Make sure that it is part of the same block, otherwise it might not be a dominating
261+
* assignment. */
262+
if (blocks[j].len == 1) {
263+
continue;
264+
}
265+
258266
if (opline->op1_type == IS_TMP_VAR &&
259267
((opline-1)->opcode == ZEND_IS_EQUAL ||
260268
(opline-1)->opcode == ZEND_IS_NOT_EQUAL ||

ext/opcache/tests/bug81015.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #81015: Opcache optimization assumes wrong part of ternary operator in if-condition
3+
--FILE--
4+
<?php
5+
6+
function ternary(bool $enabled, ?string $value): void
7+
{
8+
// the "true" part is not as trivial in the real case
9+
if ($enabled ? true : $value === null) {
10+
echo ($value ?? 'NULL') . "\n";
11+
} else {
12+
echo "INVALID\n";
13+
}
14+
}
15+
16+
ternary(true, 'value');
17+
ternary(true, null);
18+
ternary(false, 'value');
19+
ternary(false, null);
20+
21+
?>
22+
--EXPECT--
23+
value
24+
NULL
25+
INVALID
26+
NULL

0 commit comments

Comments
 (0)