Skip to content

Commit 7c29b7f

Browse files
committed
Address review
1 parent 12db8aa commit 7c29b7f

File tree

5 files changed

+30
-36
lines changed

5 files changed

+30
-36
lines changed

Zend/Optimizer/dce.c

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ static inline bool may_have_side_effects(
9595
case ZEND_DIV:
9696
case ZEND_MOD:
9797
case ZEND_BOOL_XOR:
98+
case ZEND_BOOL:
99+
case ZEND_BOOL_NOT:
98100
case ZEND_BW_NOT:
99101
case ZEND_SL:
100102
case ZEND_SR:
@@ -104,6 +106,7 @@ static inline bool may_have_side_effects(
104106
case ZEND_IS_SMALLER_OR_EQUAL:
105107
case ZEND_CASE:
106108
case ZEND_CASE_STRICT:
109+
case ZEND_CAST:
107110
case ZEND_ROPE_INIT:
108111
case ZEND_ROPE_ADD:
109112
case ZEND_INIT_ARRAY:
@@ -123,27 +126,6 @@ static inline bool may_have_side_effects(
123126
case ZEND_ARRAY_KEY_EXISTS:
124127
/* No side effects */
125128
return 0;
126-
case ZEND_CAST: {
127-
uint32_t t1 = OP1_INFO();
128-
/* Cast from NAN emits warning */
129-
if (t1 & MAY_BE_DOUBLE) {
130-
return true;
131-
}
132-
if (t1 & MAY_BE_ARRAY) {
133-
/* Array cast to string emits warning */
134-
return opline->extended_value == IS_STRING;
135-
}
136-
return false;
137-
}
138-
case ZEND_BOOL:
139-
case ZEND_BOOL_NOT: {
140-
uint32_t t1 = OP1_INFO();
141-
/* Cast from NAN emits warning */
142-
if (t1 & MAY_BE_DOUBLE) {
143-
return true;
144-
}
145-
return false;
146-
}
147129
case ZEND_FREE:
148130
return opline->extended_value == ZEND_FREE_VOID_CAST;
149131
case ZEND_ADD_ARRAY_ELEMENT:

Zend/zend_compile.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10028,17 +10028,9 @@ ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
1002810028
return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
1002910029
}
1003010030
/* Can happen when called from zend_optimizer_eval_unary_op() */
10031-
if (
10032-
opcode == ZEND_IS_EQUAL
10033-
|| opcode == ZEND_IS_NOT_EQUAL
10034-
|| opcode == ZEND_BOOL
10035-
|| opcode == ZEND_BOOL_NOT
10036-
) {
10037-
/* BW_NOT on string does not convert the string into an integer. */
10038-
if (Z_TYPE_P(op) == IS_DOUBLE) {
10039-
return true;
10040-
}
10041-
return false;
10031+
if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10032+
/* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10033+
return Z_TYPE_P(op) == IS_DOUBLE;
1004210034
}
1004310035

1004410036
return 0;

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ static void ZEND_FASTCALL zend_jit_invalid_array_access(zval *container)
25762576
zend_error(E_WARNING, "Trying to access array offset on %s", zend_zval_value_name(container));
25772577
}
25782578

2579-
static void ZEND_FASTCALL zend_jit_check_nan_to_bool_coercion(void)
2579+
static void ZEND_FASTCALL zend_jit_nan_coerced_to_type_warning(void)
25802580
{
25812581
zend_nan_coerced_to_type_warning(_IS_BOOL);
25822582
}

ext/opcache/jit/zend_jit_ir.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* +----------------------------------------------------------------------+
1717
*/
1818

19+
#include "../../../Zend/zend_types.h"
1920
#include "Zend/zend_type_info.h"
2021
#include "jit/ir/ir.h"
2122
#include "jit/ir/ir_builder.h"
@@ -7562,7 +7563,10 @@ static int zend_jit_bool_jmpznz(zend_jit_ctx *jit, const zend_op *opline, uint32
75627563
}
75637564

75647565
if (Z_MODE(op1_addr) == IS_CONST_ZVAL) {
7565-
if (zend_is_true(Z_ZV(op1_addr))) {
7566+
zval *op1 = Z_ZV(op1_addr);
7567+
/* NAN Value must cause a warning to be emitted */
7568+
// TODO function JIT does not emit warning
7569+
if ((Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1))) || zend_is_true(op1)) {
75667570
always_true = 1;
75677571
} else {
75687572
always_false = 1;
@@ -7730,10 +7734,11 @@ static int zend_jit_bool_jmpznz(zend_jit_ctx *jit, const zend_op *opline, uint32
77307734
ir_IF_TRUE(if_double);
77317735
}
77327736

7733-
ir_ref dval = jit_Z_DVAL(jit, op1_addr);ir_ref is_nan = ir_NE(dval, dval);
7737+
ir_ref dval = jit_Z_DVAL(jit, op1_addr);
7738+
ir_ref is_nan = ir_NE(dval, dval);
77347739
ir_ref if_val = ir_IF(is_nan);
77357740
ir_IF_TRUE_cold(if_val);
7736-
ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_check_nan_to_bool_coercion));
7741+
ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_nan_coerced_to_type_warning));
77377742
ir_MERGE_WITH_EMPTY_FALSE(if_val);
77387743

77397744
ref = ir_NE(dval, ir_CONST_DOUBLE(0.0));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Const NAN with bool cast should emit 1 warning
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
--EXTENSIONS--
8+
opcache
9+
--FILE--
10+
<?php
11+
echo (bool)NAN;
12+
?>
13+
--EXPECTF--
14+
Warning: unexpected NAN value was coerced to bool in %s on line 2
15+
1

0 commit comments

Comments
 (0)