Skip to content

Commit 0a8c4e0

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix phpGH-24063: SCCP merges 0.0 and -0.0 into a single constant (php#24064)
2 parents 757bc59 + 87d728d commit 0a8c4e0

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ PHP NEWS
107107
child's shadowing public property). (Ilia Alshanetsky)
108108
. Fix multiple incorrect DCE due to unsound escape analysis. (ndossche,
109109
arnaud-lb)
110+
. Fixed bug GH-24063 (OPcache optimizer folds `$cond ? -0.0 : 0.0` into a
111+
single `-0.0` constant). (lazerg)
110112

111113
- OpenSSL:
112114
. Fixed stream_socket_enable_crypto() leaving the socket non-blocking

‎Zend/Optimizer/sccp.c‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,28 @@ static void sccp_mark_feasible_successors(
19541954
scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
19551955
}
19561956

1957+
/* Unlike zend_is_identical(), this does not treat 0.0 and -0.0 as the same value.
1958+
* Returns int to be usable as compare_func_t. */
1959+
static int sccp_values_differ(const void *p1, const void *p2)
1960+
{
1961+
const zval *a = p1;
1962+
const zval *b = p2;
1963+
1964+
if (Z_TYPE_P(a) != Z_TYPE_P(b)) {
1965+
return 1;
1966+
}
1967+
if (Z_TYPE_P(a) == IS_DOUBLE) {
1968+
return memcmp(&Z_DVAL_P(a), &Z_DVAL_P(b), sizeof(double)) != 0;
1969+
}
1970+
if (Z_TYPE_P(a) == IS_ARRAY) {
1971+
return Z_ARRVAL_P(a) != Z_ARRVAL_P(b)
1972+
&& zend_hash_compare(Z_ARRVAL_P(a), Z_ARRVAL_P(b), sccp_values_differ, 1) != 0;
1973+
}
1974+
ZEND_ASSERT(IS_PARTIAL_ARRAY(a) || IS_PARTIAL_OBJECT(a)
1975+
|| ((1 << Z_TYPE_P(a)) & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_BOOL|MAY_BE_LONG|MAY_BE_STRING)));
1976+
return !zend_is_identical(a, b);
1977+
}
1978+
19571979
static void join_hash_tables(HashTable *ret, HashTable *ht1, HashTable *ht2)
19581980
{
19591981
zend_ulong index;
@@ -1966,7 +1988,7 @@ static void join_hash_tables(HashTable *ret, HashTable *ht1, HashTable *ht2)
19661988
} else {
19671989
val2 = zend_hash_index_find(ht2, index);
19681990
}
1969-
if (val2 && zend_is_identical(val1, val2)) {
1991+
if (val2 && !sccp_values_differ(val1, val2)) {
19701992
if (key) {
19711993
val1 = zend_hash_add_new(ret, key, val1);
19721994
} else {
@@ -2034,7 +2056,7 @@ static void join_phi_values(zval *a, zval *b, bool escape) {
20342056
zval_ptr_dtor_nogc(a);
20352057
MAKE_BOT(a);
20362058
}
2037-
} else if (!zend_is_identical(a, b)) {
2059+
} else if (sccp_values_differ(a, b)) {
20382060
if (join_partial_arrays(a, b) == FAILURE) {
20392061
zval_ptr_dtor_nogc(a);
20402062
MAKE_BOT(a);

‎ext/opcache/tests/opt/gh24063.phpt‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
GH-24063 (SCCP merges 0.0 and -0.0 into a single constant)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.optimization_level=-1
9+
--FILE--
10+
<?php
11+
12+
function scalar(float $n): float
13+
{
14+
$r = $n < 0 ? -0.0 : 0.0;
15+
return $r;
16+
}
17+
18+
function arr(float $n): array
19+
{
20+
$r = $n < 0 ? [-0.0] : [0.0];
21+
return $r;
22+
}
23+
24+
function partial(float $n, $x): float
25+
{
26+
$a = ['x' => $x];
27+
if ($n < 0) {
28+
$a['k'] = -0.0;
29+
} else {
30+
$a['k'] = 0.0;
31+
}
32+
return $a['k'];
33+
}
34+
35+
var_dump(scalar(1.0), scalar(-1.0));
36+
var_dump(arr(1.0)[0], arr(-1.0)[0]);
37+
var_dump(partial(1.0, 1), partial(-1.0, 1));
38+
39+
?>
40+
--EXPECT--
41+
float(0)
42+
float(-0)
43+
float(0)
44+
float(-0)
45+
float(0)
46+
float(-0)

0 commit comments

Comments
 (0)