From b4ed215299a1bd4b405959cf14541e4e2760956f Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 21 Sep 2025 23:53:16 +0100 Subject: [PATCH] core: Warn when non-representable floats are coerced to int (#19760) RFC: https://wiki.php.net/rfc/warnings-php-8-5#casting_out_of_range_floats_to_int --- Zend/Optimizer/sccp.c | 6 +- Zend/Optimizer/zend_inference.c | 1 + .../bitwise_not_precision_exception.phpt | 2 +- Zend/tests/bug46701.phpt | 10 +- Zend/tests/bug78340.phpt | 2 +- Zend/tests/falsetoarray_003.phpt | 2 +- Zend/tests/int_overflow_32bit.phpt | 11 +- Zend/tests/int_overflow_64bit.phpt | 8 +- Zend/tests/int_underflow_32bit.phpt | 10 +- ...rrayObject_container_offset_behaviour.phpt | 89 +++++++ .../array_container_offset_behaviour.phpt | 89 +++++++ Zend/tests/{ => offsets}/array_offset.phpt | 0 .../tests/{ => offsets}/array_offset_002.phpt | 2 +- .../false_container_offset_behaviour.phpt | 89 +++++++ .../null_container_offset_behaviour.phpt | 87 +++++++ .../string_container_offset_behaviour.phpt | 95 +++++++- Zend/tests/offsets/test_offset_helpers.inc | 2 +- .../runtime_compile_time_binary_operands.phpt | 2 +- .../float_to_int}/dval_to_lval_32.phpt | 13 +- .../float_to_int}/dval_to_lval_64.phpt | 11 +- .../explicit_casts_should_not_warn.phpt | 14 +- .../explicit_casts_should_not_warn_32bit.phpt | 12 +- .../non-rep-float-as-int-extra1.phpt | 19 ++ .../non-rep-float-as-int-extra2.phpt | 17 ++ .../non-rep-float-as-int-extra3.phpt | 18 ++ .../non-rep-float-as-int-extra4.phpt | 18 ++ ...g_float_does_not_fit_zend_long_arrays.phpt | 11 +- ..._float_does_not_fit_zend_long_strings.phpt | 3 + ..._does_not_fit_zend_long_strings_32bit.phpt | 3 + .../type_coercion/int_special_values.phpt | 8 +- Zend/zend_compile.c | 31 ++- Zend/zend_execute.c | 86 ++++--- Zend/zend_operators.c | 44 ++-- Zend/zend_operators.h | 30 ++- Zend/zend_vm_def.h | 10 + Zend/zend_vm_execute.h | 120 ++++++++++ ext/date/tests/bug79015.phpt | 1 + ext/dom/php_dom.c | 2 +- ext/intl/tests/gh13766.phpt | 2 +- ext/opcache/jit/zend_jit_helpers.c | 226 +++++++++--------- ext/opcache/tests/jit/add_011.phpt | 5 +- ext/opcache/tests/jit/array_elem_002.phpt | 2 +- ext/opcache/tests/jit/gh19669-001.phpt | 3 +- ext/opcache/tests/jit/gh19669-002.phpt | 3 +- .../tests/jit/reg_alloc_003_32bits.phpt | 2 +- ext/openssl/tests/openssl_decrypt_basic.phpt | 3 +- ext/standard/array.c | 18 +- .../gettype_settype_variation2.phpt | 12 + .../tests/general_functions/intval.phpt | 24 +- ext/standard/tests/math/bug30695.phpt | 16 +- ext/standard/tests/strings/bug47842.phpt | 4 +- ext/standard/tests/strings/pack.phpt | 22 +- ext/standard/tests/strings/pack64.phpt | 18 +- .../tests/strings/vprintf_variation12.phpt | 8 +- .../tests/strings/vprintf_variation14.phpt | 8 +- .../tests/strings/vprintf_variation15.phpt | 6 +- .../strings/vprintf_variation15_64bit.phpt | 4 +- .../tests/strings/vprintf_variation16.phpt | 8 +- .../tests/strings/vprintf_variation4.phpt | 4 +- main/php_variables.c | 2 +- tests/lang/bug27354.phpt | 2 +- .../operators/bitwiseNot_basiclong_64bit.phpt | 2 +- 62 files changed, 1107 insertions(+), 275 deletions(-) rename Zend/tests/{ => offsets}/array_offset.phpt (100%) rename Zend/tests/{ => offsets}/array_offset_002.phpt (80%) rename Zend/tests/{ => type_coercion/float_to_int}/dval_to_lval_32.phpt (51%) rename Zend/tests/{ => type_coercion/float_to_int}/dval_to_lval_64.phpt (52%) create mode 100644 Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra1.phpt create mode 100644 Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra2.phpt create mode 100644 Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra3.phpt create mode 100644 Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra4.phpt diff --git a/Zend/Optimizer/sccp.c b/Zend/Optimizer/sccp.c index c1faf2a3fbe..9b8216e589b 100644 --- a/Zend/Optimizer/sccp.c +++ b/Zend/Optimizer/sccp.c @@ -371,7 +371,7 @@ static inline zend_result fetch_array_elem(zval **result, zval *op1, zval *op2) *result = zend_hash_index_find(Z_ARR_P(op1), Z_LVAL_P(op2)); return SUCCESS; case IS_DOUBLE: { - zend_long lval = zend_dval_to_lval(Z_DVAL_P(op2)); + zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(op2)); if (!zend_is_long_compatible(Z_DVAL_P(op2), lval)) { return FAILURE; } @@ -459,7 +459,7 @@ static inline zend_result ct_eval_del_array_elem(zval *result, const zval *key) zend_hash_index_del(Z_ARR_P(result), Z_LVAL_P(key)); break; case IS_DOUBLE: { - zend_long lval = zend_dval_to_lval(Z_DVAL_P(key)); + zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key)); if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) { return FAILURE; } @@ -504,7 +504,7 @@ static inline zend_result ct_eval_add_array_elem(zval *result, zval *value, cons value = zend_hash_index_update(Z_ARR_P(result), Z_LVAL_P(key), value); break; case IS_DOUBLE: { - zend_long lval = zend_dval_to_lval(Z_DVAL_P(key)); + zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key)); if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) { return FAILURE; } diff --git a/Zend/Optimizer/zend_inference.c b/Zend/Optimizer/zend_inference.c index 62e04cb0e30..7f84ed55de6 100644 --- a/Zend/Optimizer/zend_inference.c +++ b/Zend/Optimizer/zend_inference.c @@ -5283,6 +5283,7 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op case ZEND_CAST: switch (opline->extended_value) { case IS_LONG: + return (t1 & (MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_OBJECT)); case IS_DOUBLE: return (t1 & MAY_BE_OBJECT); case IS_STRING: diff --git a/Zend/tests/bitwise_not_precision_exception.phpt b/Zend/tests/bitwise_not_precision_exception.phpt index fa821100464..e28bf8f4e17 100644 --- a/Zend/tests/bitwise_not_precision_exception.phpt +++ b/Zend/tests/bitwise_not_precision_exception.phpt @@ -12,4 +12,4 @@ try { } ?> --EXPECT-- -Implicit conversion from float INF to int loses precision +The float INF is not representable as an int, cast occurred diff --git a/Zend/tests/bug46701.phpt b/Zend/tests/bug46701.phpt index e7cc823d85c..620d5209443 100644 --- a/Zend/tests/bug46701.phpt +++ b/Zend/tests/bug46701.phpt @@ -27,11 +27,11 @@ new foo; ?> --EXPECTF-- -Deprecated: Implicit conversion from float 3428599296 to int loses precision in %s on line %d +Warning: The float 3428599296 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 3459455488 to int loses precision in %s on line %d +Warning: The float 3459455488 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 3459616768 to int loses precision in %s on line %d +Warning: The float 3459616768 is not representable as an int, cast occurred in %s on line %d array(3) { [-866368000]=> int(1) @@ -41,10 +41,10 @@ array(3) { int(3) } -Deprecated: Implicit conversion from float 3459455488 to int loses precision in %s on line %d +Warning: The float 3459455488 is not representable as an int, cast occurred in %s on line %d int(2) -Deprecated: Implicit conversion from float 3459616768 to int loses precision in %s on line %d +Warning: The float 3459616768 is not representable as an int, cast occurred in %s on line %d array(1) { [-835350528]=> int(3) diff --git a/Zend/tests/bug78340.phpt b/Zend/tests/bug78340.phpt index 4012e83af6a..22ea0c19353 100644 --- a/Zend/tests/bug78340.phpt +++ b/Zend/tests/bug78340.phpt @@ -32,7 +32,7 @@ class lib { function stream_stat() { return [ - 'dev' => 3632233996, + 'dev' => PHP_INT_MAX, 'size' => strlen($this->bytes), 'ino' => $this->ino ]; diff --git a/Zend/tests/falsetoarray_003.phpt b/Zend/tests/falsetoarray_003.phpt index 11b32771e1f..117e443ef95 100644 --- a/Zend/tests/falsetoarray_003.phpt +++ b/Zend/tests/falsetoarray_003.phpt @@ -11,6 +11,6 @@ $a=[]; ?> DONE --EXPECTF-- -Err: Implicit conversion from float %f to int loses precision +Err: The float %f is not representable as an int, cast occurred Err: Undefined array key %i DONE diff --git a/Zend/tests/int_overflow_32bit.phpt b/Zend/tests/int_overflow_32bit.phpt index 15dce6eea7d..e337932bcd5 100644 --- a/Zend/tests/int_overflow_32bit.phpt +++ b/Zend/tests/int_overflow_32bit.phpt @@ -20,10 +20,19 @@ foreach ($doubles as $d) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- +Warning: The float 2147483648 is not representable as an int, cast occurred in %s on line %d int(-2147483648) + +Warning: The float 2147483649 is not representable as an int, cast occurred in %s on line %d int(-2147483647) + +Warning: The float 2147483658 is not representable as an int, cast occurred in %s on line %d int(-2147483638) + +Warning: The float 2147483748 is not representable as an int, cast occurred in %s on line %d int(-2147483548) + +Warning: The float 2147484648 is not representable as an int, cast occurred in %s on line %d int(-2147482648) Done diff --git a/Zend/tests/int_overflow_64bit.phpt b/Zend/tests/int_overflow_64bit.phpt index 7c541250205..cc14e14949c 100644 --- a/Zend/tests/int_overflow_64bit.phpt +++ b/Zend/tests/int_overflow_64bit.phpt @@ -22,10 +22,16 @@ foreach ($doubles as $d) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- int(9223372036854775807) + +Warning: The float %f is not representable as an int, cast occurred in %s on line %d int(-9223372036854775808) + +Warning: The float %f is not representable as an int, cast occurred in %s on line %d int(-9223372036854775808) + +Warning: The float %f is not representable as an int, cast occurred in %s on line %d int(0) int(-9223372036854775808) int(-9223372036854775808) diff --git a/Zend/tests/int_underflow_32bit.phpt b/Zend/tests/int_underflow_32bit.phpt index 71464a758ae..88c0e79834b 100644 --- a/Zend/tests/int_underflow_32bit.phpt +++ b/Zend/tests/int_underflow_32bit.phpt @@ -20,10 +20,18 @@ foreach ($doubles as $d) { echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- int(-2147483648) + +Warning: The float -2147483649 is not representable as an int, cast occurred in %s on line %d int(2147483647) + +Warning: The float -2147483658 is not representable as an int, cast occurred in %s on line %d int(2147483638) + +Warning: The float -2147483748 is not representable as an int, cast occurred in %s on line %d int(2147483548) + +Warning: The float -2147484648 is not representable as an int, cast occurred in %s on line %d int(2147482648) Done diff --git a/Zend/tests/offsets/ArrayObject_container_offset_behaviour.phpt b/Zend/tests/offsets/ArrayObject_container_offset_behaviour.phpt index c44c2df24f7..bcfbd4b3173 100644 --- a/Zend/tests/offsets/ArrayObject_container_offset_behaviour.phpt +++ b/Zend/tests/offsets/ArrayObject_container_offset_behaviour.phpt @@ -132,6 +132,93 @@ OUTPUT; $EXPECTED_OUTPUT_FLOAT_OFFSETS_REGEX = '/^' . expectf_to_regex(EXPECTF_OUTPUT_FLOAT_OFFSETS) . '$/s'; +const EXPECTF_OUTPUT_FLOAT_OOB_OFFSETS = << --EXPECT-- -Err: Implicit conversion from float 1.0E+20 to int loses precision +Err: The float 1.0E+20 is not representable as an int, cast occurred array(0) { } diff --git a/Zend/tests/offsets/false_container_offset_behaviour.phpt b/Zend/tests/offsets/false_container_offset_behaviour.phpt index 736e51830a4..0647400baee 100644 --- a/Zend/tests/offsets/false_container_offset_behaviour.phpt +++ b/Zend/tests/offsets/false_container_offset_behaviour.phpt @@ -135,6 +135,93 @@ OUTPUT; $EXPECTED_OUTPUT_FLOAT_OFFSETS_REGEX = '/^' . expectf_to_regex(EXPECTF_OUTPUT_FLOAT_OFFSETS) . '$/s'; +const EXPECTF_OUTPUT_FLOAT_OOB_OFFSETS = << '\d+', '%x' => '[0-9a-fA-F]+', '%f' => '[+-]?(?:\d+|(?=\.\d))(?:\.\d+)?(?:[Ee][+-]?\d+)?', - '%F' => '([+-]?(?:\d+|(?=\.\d))(?:\.\d+)?(?:[Ee][+-]?\d+)?)|(NAN)|(INF)', + '%F' => '([+-]?(?:\d+|(?=\.\d))(?:\.\d+)?(?:[Ee][+-]?\d+)?)|(NAN)|(INF)|([+-]?\d+)', '%c' => '.', '%0' => '\x00', ]); diff --git a/Zend/tests/runtime_compile_time_binary_operands.phpt b/Zend/tests/runtime_compile_time_binary_operands.phpt index e18134283e8..948246f73b4 100644 --- a/Zend/tests/runtime_compile_time_binary_operands.phpt +++ b/Zend/tests/runtime_compile_time_binary_operands.phpt @@ -8,7 +8,7 @@ if (getenv("SKIP_SLOW_TESTS")) die('skip slow test'); ?> --FILE-- ---EXPECT-- +--EXPECTF-- +Warning: The float -4.000000000000001E+21 is not representable as an int, cast occurred in %s on line %d int(-2056257536) + +Warning: The float -4.0000000000000005E+21 is not representable as an int, cast occurred in %s on line %d int(-2055733248) + +Warning: The float -4.0E+21 is not representable as an int, cast occurred in %s on line %d int(-2055208960) + +Warning: The float -3.9999999999999995E+21 is not representable as an int, cast occurred in %s on line %d int(-2054684672) + +Warning: The float -3.999999999999999E+21 is not representable as an int, cast occurred in %s on line %d int(-2054160384) + +Warning: The float -2147483649.8 is not representable as an int, cast occurred in %s on line %d int(2147483647) diff --git a/Zend/tests/dval_to_lval_64.phpt b/Zend/tests/type_coercion/float_to_int/dval_to_lval_64.phpt similarity index 52% rename from Zend/tests/dval_to_lval_64.phpt rename to Zend/tests/type_coercion/float_to_int/dval_to_lval_64.phpt index b54f861bf55..993950534fc 100644 --- a/Zend/tests/dval_to_lval_64.phpt +++ b/Zend/tests/type_coercion/float_to_int/dval_to_lval_64.phpt @@ -21,9 +21,18 @@ if (PHP_INT_SIZE != 8) } ?> ---EXPECT-- +--EXPECTF-- +Warning: The float -4.000000000000001E+21 is not representable as an int, cast occurred in %s on line %d int(2943463994971652096) + +Warning: The float -4.0000000000000005E+21 is not representable as an int, cast occurred in %s on line %d int(2943463994972176384) + +Warning: The float -4.0E+21 is not representable as an int, cast occurred in %s on line %d int(2943463994972700672) + +Warning: The float -3.9999999999999995E+21 is not representable as an int, cast occurred in %s on line %d int(2943463994973224960) + +Warning: The float -3.999999999999999E+21 is not representable as an int, cast occurred in %s on line %d int(2943463994973749248) diff --git a/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn.phpt b/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn.phpt index 71c68e57574..2b876597b9b 100644 --- a/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn.phpt +++ b/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn.phpt @@ -1,5 +1,5 @@ --TEST-- -Explicit (int) cast must not warn +Explicit (int) cast must not warn if value is representable --SKIPIF-- ---EXPECT-- +--EXPECTF-- int(3) int(3) + +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float 1.0E+301 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float NAN is not representable as an int, cast occurred in %s on line %d int(0) int(3) int(3) + +Warning: The float-string "1.0E+121" is not representable as an int, cast occurred in %s on line %d int(9223372036854775807) + +Warning: The float-string "1.0E+301" is not representable as an int, cast occurred in %s on line %d int(9223372036854775807) int(0) diff --git a/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn_32bit.phpt b/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn_32bit.phpt index fee011df06e..b34e762dfff 100644 --- a/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn_32bit.phpt +++ b/Zend/tests/type_coercion/float_to_int/explicit_casts_should_not_warn_32bit.phpt @@ -25,14 +25,24 @@ foreach($values as $value) { } ?> ---EXPECT-- +--EXPECTF-- int(3) int(3) + +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float 1.0E+301 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float NAN is not representable as an int, cast occurred in %s on line %d int(0) int(3) int(3) + +Warning: The float-string "1.0E+121" is not representable as an int, cast occurred in %s on line %d int(2147483647) + +Warning: The float-string "1.0E+301" is not representable as an int, cast occurred in %s on line %d int(2147483647) int(0) diff --git a/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra1.phpt b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra1.phpt new file mode 100644 index 00000000000..aacf0dc8551 --- /dev/null +++ b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra1.phpt @@ -0,0 +1,19 @@ +--TEST-- +Non rep float string to int conversions should not crash when modified +--FILE-- + +--EXPECTF-- +The float-string "1.0E+4%d" is not representable as an int, cast occurred +Implicit conversion from float-string "1.0E+4%d" to int loses precision +int(%d) diff --git a/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra2.phpt b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra2.phpt new file mode 100644 index 00000000000..0e96acfd182 --- /dev/null +++ b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra2.phpt @@ -0,0 +1,17 @@ +--TEST-- +Non rep float string to int conversions should not crash when modified +--FILE-- + +--EXPECT-- +The float 1.0E+42 is not representable as an int, cast occurred diff --git a/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra3.phpt b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra3.phpt new file mode 100644 index 00000000000..0bfbeb01a2d --- /dev/null +++ b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra3.phpt @@ -0,0 +1,18 @@ +--TEST-- +Non rep float string to int conversions should not crash when modified +--FILE-- + +--EXPECT-- +The float 1.0E+42 is not representable as an int, cast occurred +bool(false) diff --git a/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra4.phpt b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra4.phpt new file mode 100644 index 00000000000..8134ce08d2e --- /dev/null +++ b/Zend/tests/type_coercion/float_to_int/non-rep-float-as-int-extra4.phpt @@ -0,0 +1,18 @@ +--TEST-- +Non rep float string to int conversions should not crash when modified +--FILE-- + +--EXPECT-- +The float 1.0E+42 is not representable as an int, cast occurred +bool(false) diff --git a/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_arrays.phpt b/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_arrays.phpt index bb27d1bb49d..779e103e6f8 100644 --- a/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_arrays.phpt +++ b/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_arrays.phpt @@ -23,12 +23,15 @@ var_dump($array[$string_float]); ?> --EXPECTF-- +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float-string "1.0E+121" is not representable as an int, cast occurred in %s on line %d bool(true) -Deprecated: Implicit conversion from float 1.0E+121 to int loses precision in %s on line %d +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 1.0E+121 to int loses precision in %s on line %d +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d array(2) { [0]=> string(11) "Large float" @@ -42,13 +45,13 @@ array(2) { string(18) "String large float" } -Deprecated: Implicit conversion from float 1.0E+121 to int loses precision in %s on line %d +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d string(1) "0" Warning: Undefined array key "1.0E+121" in %s on line %d NULL -Deprecated: Implicit conversion from float 1.0E+121 to int loses precision in %s on line %d +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d string(1) "0" Warning: Undefined array key "1.0E+121" in %s on line %d diff --git a/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings.phpt b/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings.phpt index 0c63cba1f5b..b4651d28ac4 100644 --- a/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings.phpt +++ b/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings.phpt @@ -75,7 +75,10 @@ var_dump($string); ?> --EXPECTF-- +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float-string "1.0E+121" is not representable as an int, cast occurred in %s on line %d int(9223372036854775807) Attempt to read Float diff --git a/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings_32bit.phpt b/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings_32bit.phpt index cf2e396c311..3ec2c62cacd 100644 --- a/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings_32bit.phpt +++ b/Zend/tests/type_coercion/float_to_int/warning_float_does_not_fit_zend_long_strings_32bit.phpt @@ -75,7 +75,10 @@ var_dump($string); ?> --EXPECTF-- +Warning: The float 1.0E+121 is not representable as an int, cast occurred in %s on line %d int(0) + +Warning: The float-string "1.0E+121" is not representable as an int, cast occurred in %s on line %d int(2147483647) Attempt to read Float diff --git a/Zend/tests/type_coercion/int_special_values.phpt b/Zend/tests/type_coercion/int_special_values.phpt index eedb9ab2c02..e536ff988e0 100644 --- a/Zend/tests/type_coercion/int_special_values.phpt +++ b/Zend/tests/type_coercion/int_special_values.phpt @@ -17,14 +17,18 @@ foreach($values as $value) { echo PHP_EOL; } ?> ---EXPECT-- +--EXPECTF-- float(0) int(0) float(INF) + +Warning: The float INF is not representable as an int, cast occurred in %s on line %d int(0) float(-INF) + +Warning: The float -INF is not representable as an int, cast occurred in %s on line %d int(0) float(0) @@ -34,4 +38,6 @@ float(-0) int(0) float(NAN) + +Warning: The float NAN is not representable as an int, cast occurred in %s on line %d int(0) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index f43fdd7b318..b46b5d5bff8 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -9939,14 +9939,14 @@ ZEND_API bool zend_is_op_long_compatible(const zval *op) } if (Z_TYPE_P(op) == IS_DOUBLE - && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval(Z_DVAL_P(op)))) { + && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) { return false; } if (Z_TYPE_P(op) == IS_STRING) { double dval = 0; uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval); - if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval(dval)))) { + if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) { return false; } } @@ -9995,11 +9995,23 @@ ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, co return 1; } - if ((opcode == ZEND_MOD && zval_get_long(op2) == 0) - || (opcode == ZEND_DIV && zval_get_double(op2) == 0.0)) { + /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */ + if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR + || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) { + return !zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2); + } + + if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) { /* Division by zero throws an error. */ return 1; } + + /* Mod is an operation that will cast float/float-strings to integers which might + produce float to int incompatible errors, and also cannot be divided by 0 */ + if (opcode == ZEND_MOD) { + return !zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0; + } + if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) { /* 0 ** (<0) throws a division by zero error. */ return 1; @@ -10009,12 +10021,6 @@ ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, co return 1; } - /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */ - if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR - || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR || opcode == ZEND_MOD) { - return !zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2); - } - return 0; } /* }}} */ @@ -10166,7 +10172,7 @@ static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value); break; case IS_DOUBLE: { - zend_long lval = zend_dval_to_lval(Z_DVAL_P(key)); + zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key)); /* Incompatible float will generate an error, leave this to run-time */ if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) { zval_ptr_dtor_nogc(value); @@ -12057,6 +12063,9 @@ bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1) ZVAL_BOOL(result, zval_is_true(op1)); return true; case IS_LONG: + if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) { + return false; + } ZVAL_LONG(result, zval_get_long(op1)); return true; case IS_DOUBLE: diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 940c2f98b38..5665cc0c3f7 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -1732,10 +1732,13 @@ try_again: zend_illegal_string_offset(dim, type); return 0; } + case IS_DOUBLE: + /* Suppress potential double warning */ + zend_error(E_WARNING, "String offset cast occurred"); + return zend_dval_to_lval_silent(Z_DVAL_P(dim)); case IS_UNDEF: ZVAL_UNDEFINED_OP2(); ZEND_FALLTHROUGH; - case IS_DOUBLE: case IS_NULL: case IS_FALSE: case IS_TRUE: @@ -2668,21 +2671,18 @@ static zend_never_inline uint8_t slow_index_convert(HashTable *ht, const zval *d value->str = ZSTR_EMPTY_ALLOC(); return IS_STRING; case IS_DOUBLE: - value->lval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), value->lval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { - zend_array_destroy(ht); - return IS_NULL; - } - if (EG(exception)) { - return IS_NULL; - } + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + value->lval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + return IS_NULL; + } + if (EG(exception)) { + return IS_NULL; } return IS_LONG; case IS_RESOURCE: @@ -2753,23 +2753,20 @@ static zend_never_inline uint8_t slow_index_convert_w(HashTable *ht, const zval value->str = ZSTR_EMPTY_ALLOC(); return IS_STRING; case IS_DOUBLE: - value->lval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), value->lval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) { - if (!GC_REFCOUNT(ht)) { - zend_array_destroy(ht); - } - return IS_NULL; - } - if (EG(exception)) { - return IS_NULL; + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + value->lval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) { + if (!GC_REFCOUNT(ht)) { + zend_array_destroy(ht); } + return IS_NULL; + } + if (EG(exception)) { + return IS_NULL; } return IS_LONG; case IS_RESOURCE: @@ -3129,6 +3126,11 @@ try_string_offset: return; } } + /* To prevent double warning */ + if (Z_TYPE_P(dim) == IS_DOUBLE) { + offset = zend_dval_to_lval_silent(Z_DVAL_P(dim)); + goto out; + } break; case IS_REFERENCE: dim = Z_REFVAL_P(dim); @@ -3239,7 +3241,17 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable zend_ulong hval; if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + return NULL; + } + if (EG(exception)) { + return NULL; + } num_idx: return zend_hash_index_find(ht, hval); } else if (Z_TYPE_P(offset) == IS_NULL) { @@ -3378,7 +3390,17 @@ num_key: key = Z_REFVAL_P(key); goto try_again; } else if (Z_TYPE_P(key) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(key)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + return false; + } + if (EG(exception)) { + return false; + } goto num_key; } else if (Z_TYPE_P(key) == IS_FALSE) { hval = 0; diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index e6e0c82c79f..91421b45fdf 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -387,12 +387,9 @@ try_again: return 1; case IS_DOUBLE: { double dval = Z_DVAL_P(op); - zend_long lval = zend_dval_to_lval(dval); - if (!zend_is_long_compatible(dval, lval)) { - zend_incompatible_double_to_long_error(dval); - if (UNEXPECTED(EG(exception))) { - *failed = 1; - } + zend_long lval = zend_dval_to_lval_safe(dval); + if (UNEXPECTED(EG(exception))) { + *failed = 1; } return lval; } @@ -418,6 +415,8 @@ try_again: zend_error(E_WARNING, "A non-numeric value encountered"); if (UNEXPECTED(EG(exception))) { *failed = 1; + zend_tmp_string_release(op_str); + return 0; } } if (EXPECTED(type == IS_LONG)) { @@ -428,14 +427,18 @@ try_again: * We use use saturating conversion to emulate strtol()'s * behaviour. */ - lval = zend_dval_to_lval_cap(dval); + if (op_str == NULL) { + /* zend_dval_to_lval_cap() can emit a warning so always do the copy here */ + op_str = zend_string_copy(Z_STR_P(op)); + } + lval = zend_dval_to_lval_cap(dval, op_str); if (!zend_is_long_compatible(dval, lval)) { - zend_incompatible_string_to_long_error(op_str ? op_str : Z_STR_P(op)); + zend_incompatible_string_to_long_error(op_str); if (UNEXPECTED(EG(exception))) { *failed = 1; } } - zend_tmp_string_release(op_str); + zend_string_release(op_str); return lval; } } @@ -911,6 +914,14 @@ ZEND_API void ZEND_COLD zend_incompatible_string_to_long_error(const zend_string { zend_error(E_DEPRECATED, "Implicit conversion from float-string \"%s\" to int loses precision", ZSTR_VAL(s)); } +ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d) +{ + zend_error_unchecked(E_WARNING, "The float %.*H is not representable as an int, cast occurred", -1, d); +} +ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s) +{ + zend_error_unchecked(E_WARNING, "The float-string \"%s\" is not representable as an int, cast occurred", ZSTR_VAL(s)); +} ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict) /* {{{ */ { @@ -952,7 +963,7 @@ try_again: * behaviour. */ /* Most usages are expected to not be (int) casts */ - lval = zend_dval_to_lval_cap(dval); + lval = zend_dval_to_lval_cap(dval, Z_STR_P(op)); if (UNEXPECTED(is_strict)) { if (!zend_is_long_compatible(dval, lval)) { zend_incompatible_string_to_long_error(Z_STR_P(op)); @@ -1613,15 +1624,12 @@ try_again: ZVAL_LONG(result, ~Z_LVAL_P(op1)); return SUCCESS; case IS_DOUBLE: { - zend_long lval = zend_dval_to_lval(Z_DVAL_P(op1)); - if (!zend_is_long_compatible(Z_DVAL_P(op1), lval)) { - zend_incompatible_double_to_long_error(Z_DVAL_P(op1)); - if (EG(exception)) { - if (result != op1) { - ZVAL_UNDEF(result); - } - return FAILURE; + zend_long lval = zend_dval_to_lval_safe(Z_DVAL_P(op1)); + if (EG(exception)) { + if (result != op1) { + ZVAL_UNDEF(result); } + return FAILURE; } ZVAL_LONG(result, ~lval); return SUCCESS; diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h index 63b0fb62e49..e821827f57f 100644 --- a/Zend/zend_operators.h +++ b/Zend/zend_operators.h @@ -115,11 +115,28 @@ ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const # define ZEND_DOUBLE_FITS_LONG(d) (!((d) >= (double)ZEND_LONG_MAX || (d) < (double)ZEND_LONG_MIN)) #endif +ZEND_API void zend_incompatible_double_to_long_error(double d); +ZEND_API void zend_incompatible_string_to_long_error(const zend_string *s); +ZEND_API void ZEND_COLD zend_oob_double_to_long_error(double d); +ZEND_API void ZEND_COLD zend_oob_string_to_long_error(const zend_string *s); + ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d); static zend_always_inline zend_long zend_dval_to_lval(double d) { - if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) { + if (UNEXPECTED(!zend_finite(d))) { + zend_oob_double_to_long_error(d); + return 0; + } else if (!ZEND_DOUBLE_FITS_LONG(d)) { + zend_oob_double_to_long_error(d); + return zend_dval_to_lval_slow(d); + } + return (zend_long)d; +} + +static zend_always_inline zend_long zend_dval_to_lval_silent(double d) +{ + if (UNEXPECTED(!zend_finite(d))) { return 0; } else if (!ZEND_DOUBLE_FITS_LONG(d)) { return zend_dval_to_lval_slow(d); @@ -128,11 +145,13 @@ static zend_always_inline zend_long zend_dval_to_lval(double d) } /* Used to convert a string float to integer during an (int) cast */ -static zend_always_inline zend_long zend_dval_to_lval_cap(double d) +static zend_always_inline zend_long zend_dval_to_lval_cap(double d, const zend_string *s) { - if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) { + if (UNEXPECTED(!zend_finite(d))) { + zend_oob_string_to_long_error(s); return 0; } else if (!ZEND_DOUBLE_FITS_LONG(d)) { + zend_oob_string_to_long_error(s); return (d > 0 ? ZEND_LONG_MAX : ZEND_LONG_MIN); } return (zend_long)d; @@ -143,13 +162,10 @@ static zend_always_inline bool zend_is_long_compatible(double d, zend_long l) { return (double)l == d; } -ZEND_API void zend_incompatible_double_to_long_error(double d); -ZEND_API void zend_incompatible_string_to_long_error(const zend_string *s); - static zend_always_inline zend_long zend_dval_to_lval_safe(double d) { zend_long l = zend_dval_to_lval(d); - if (!zend_is_long_compatible(d, l)) { + if (!zend_is_long_compatible(d, l) && ZEND_DOUBLE_FITS_LONG(d)) { zend_incompatible_double_to_long_error(d); } return l; diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 3d6463d0648..3ebf816cf38 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -6751,7 +6751,17 @@ ZEND_VM_C_LABEL(num_index_dim): offset = Z_REFVAL_P(offset); ZEND_VM_C_GOTO(offset_again); } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } ZEND_VM_C_GOTO(num_index_dim); } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 33c8e9c2231..b0d6f2bc33d 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -26627,7 +26627,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -29168,7 +29178,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -33687,7 +33707,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -46191,7 +46221,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -50020,7 +50060,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -55769,7 +55819,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -81788,7 +81848,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -84329,7 +84399,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -88848,7 +88928,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -101352,7 +101442,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -105181,7 +105281,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); @@ -110828,7 +110938,17 @@ num_index_dim: offset = Z_REFVAL_P(offset); goto offset_again; } else if (Z_TYPE_P(offset) == IS_DOUBLE) { + /* The array may be destroyed while throwing a warning in case the float is not representable as an int. + * Temporarily increase the refcount to detect this situation. */ + GC_TRY_ADDREF(ht); hval = zend_dval_to_lval_safe(Z_DVAL_P(offset)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + break; + } + if (EG(exception)) { + break; + } goto num_index_dim; } else if (Z_TYPE_P(offset) == IS_NULL) { key = ZSTR_EMPTY_ALLOC(); diff --git a/ext/date/tests/bug79015.phpt b/ext/date/tests/bug79015.phpt index 99cb03f75d5..b2f1a63fa4c 100644 --- a/ext/date/tests/bug79015.phpt +++ b/ext/date/tests/bug79015.phpt @@ -6,6 +6,7 @@ $payload = 'O:12:"DateInterval":9:{s:1:"y";i:1;s:1:"m";i:0;s:1:"d";i:4;s:1:"h";i var_dump(unserialize($payload)); ?> --EXPECTF-- +Warning: The float 9.99999999999E+18 is not representable as an int, cast occurred in %s on line %d object(DateInterval)#%d (%d) { ["y"]=> int(1) diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c index ee99f97f354..095d07e875e 100644 --- a/ext/dom/php_dom.c +++ b/ext/dom/php_dom.c @@ -2261,7 +2261,7 @@ static bool dom_nodemap_or_nodelist_process_offset_as_named(zval *offset, zend_l if (0 == (is_numeric_string_type = is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), lval, &dval, true))) { return true; } else if (is_numeric_string_type == IS_DOUBLE) { - *lval = zend_dval_to_lval_cap(dval); + *lval = zend_dval_to_lval_cap(dval, Z_STR_P(offset)); } } else { *lval = zval_get_long(offset); diff --git a/ext/intl/tests/gh13766.phpt b/ext/intl/tests/gh13766.phpt index 70567fa8605..9ed8d985de4 100644 --- a/ext/intl/tests/gh13766.phpt +++ b/ext/intl/tests/gh13766.phpt @@ -32,4 +32,4 @@ int(%d) string(19) "America/Los_Angeles" IntlDateFormatter::parseToCalendar(): Argument #2 ($offset) must be of type int, string given -Deprecated: Implicit conversion from float %r(1\.4757395258967641E\+20|34359738352)%r to int loses precision in %s on line %d +Warning: The float %r(1\.4757395258967641E\+20|34359738352)%r is not representable as an int, cast occurred in %s on line %d diff --git a/ext/opcache/jit/zend_jit_helpers.c b/ext/opcache/jit/zend_jit_helpers.c index ce48d102234..d49d5ee3007 100644 --- a/ext/opcache/jit/zend_jit_helpers.c +++ b/ext/opcache/jit/zend_jit_helpers.c @@ -513,33 +513,30 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_r_helper(zend_array *ht, zval *dim, } return; case IS_DOUBLE: - hval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - execute_data = EG(current_execute_data); - opline = EX(opline); - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { - zend_array_destroy(ht); - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { - if (EG(exception)) { - ZVAL_UNDEF(EX_VAR(opline->result.var)); - } else { - ZVAL_NULL(EX_VAR(opline->result.var)); - } - } - return; - } - if (EG(exception)) { - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + execute_data = EG(current_execute_data); + opline = EX(opline); + hval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + if (EG(exception)) { ZVAL_UNDEF(EX_VAR(opline->result.var)); + } else { + ZVAL_NULL(EX_VAR(opline->result.var)); } - return; } + return; + } + if (EG(exception)) { + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + ZVAL_UNDEF(EX_VAR(opline->result.var)); + } + return; } goto num_index; case IS_RESOURCE: @@ -663,33 +660,30 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_is_helper(zend_array *ht, zval *dim return; case IS_DOUBLE: - hval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - execute_data = EG(current_execute_data); - opline = EX(opline); - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { - zend_array_destroy(ht); - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { - if (EG(exception)) { - ZVAL_UNDEF(EX_VAR(opline->result.var)); - } else { - ZVAL_NULL(EX_VAR(opline->result.var)); - } - } - return; - } - if (EG(exception)) { - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + execute_data = EG(current_execute_data); + opline = EX(opline); + hval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + if (EG(exception)) { ZVAL_UNDEF(EX_VAR(opline->result.var)); + } else { + ZVAL_NULL(EX_VAR(opline->result.var)); } - return; } + return; + } + if (EG(exception)) { + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + ZVAL_UNDEF(EX_VAR(opline->result.var)); + } + return; } goto num_index; case IS_RESOURCE: @@ -799,21 +793,18 @@ static int ZEND_FASTCALL zend_jit_fetch_dim_isset_helper(zend_array *ht, zval *d return result; } case IS_DOUBLE: - hval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { - zend_array_destroy(ht); - return 0; - } - if (EG(exception)) { - return 0; - } + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + hval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) { + zend_array_destroy(ht); + return 0; + } + if (EG(exception)) { + return 0; } goto num_index; case IS_RESOURCE: @@ -933,35 +924,32 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_rw_helper(zend_array *ht, zval *di offset_key = ZSTR_EMPTY_ALLOC(); goto str_index; case IS_DOUBLE: - hval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - execute_data = EG(current_execute_data); - opline = EX(opline); - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) { - if (!GC_REFCOUNT(ht)) { - zend_array_destroy(ht); - } - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { - if (EG(exception)) { - ZVAL_UNDEF(EX_VAR(opline->result.var)); - } else { - ZVAL_NULL(EX_VAR(opline->result.var)); - } - } - return NULL; + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + execute_data = EG(current_execute_data); + opline = EX(opline); + hval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) { + if (!GC_REFCOUNT(ht)) { + zend_array_destroy(ht); } - if (EG(exception)) { - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + if (EG(exception)) { ZVAL_UNDEF(EX_VAR(opline->result.var)); + } else { + ZVAL_NULL(EX_VAR(opline->result.var)); } - return NULL; } + return NULL; + } + if (EG(exception)) { + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + ZVAL_UNDEF(EX_VAR(opline->result.var)); + } + return NULL; } goto num_index; case IS_RESOURCE: @@ -1096,35 +1084,32 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim offset_key = ZSTR_EMPTY_ALLOC(); goto str_index; case IS_DOUBLE: - hval = zend_dval_to_lval(Z_DVAL_P(dim)); - if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) { - /* The array may be destroyed while throwing the notice. - * Temporarily increase the refcount to detect this situation. */ - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { - GC_ADDREF(ht); - } - execute_data = EG(current_execute_data); - opline = EX(opline); - zend_incompatible_double_to_long_error(Z_DVAL_P(dim)); - if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) { - if (!GC_REFCOUNT(ht)) { - zend_array_destroy(ht); - } - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { - if (EG(exception)) { - ZVAL_UNDEF(EX_VAR(opline->result.var)); - } else { - ZVAL_NULL(EX_VAR(opline->result.var)); - } - } - return NULL; + /* The array may be destroyed while throwing the notice. + * Temporarily increase the refcount to detect this situation. */ + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { + GC_ADDREF(ht); + } + execute_data = EG(current_execute_data); + opline = EX(opline); + hval = zend_dval_to_lval_safe(Z_DVAL_P(dim)); + if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) { + if (!GC_REFCOUNT(ht)) { + zend_array_destroy(ht); } - if (EG(exception)) { - if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + if (EG(exception)) { ZVAL_UNDEF(EX_VAR(opline->result.var)); + } else { + ZVAL_NULL(EX_VAR(opline->result.var)); } - return NULL; } + return NULL; + } + if (EG(exception)) { + if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { + ZVAL_UNDEF(EX_VAR(opline->result.var)); + } + return NULL; } goto num_index; case IS_RESOURCE: @@ -1211,10 +1196,13 @@ try_again: zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), dim, BP_VAR_R); return 0; } + case IS_DOUBLE: + /* Suppress potential double warning */ + zend_error(E_WARNING, "String offset cast occurred"); + return zend_dval_to_lval_silent(Z_DVAL_P(dim)); case IS_UNDEF: zend_jit_undefined_op_helper(EG(current_execute_data)->opline->op2.var); ZEND_FALLTHROUGH; - case IS_DOUBLE: case IS_NULL: case IS_FALSE: case IS_TRUE: @@ -1286,14 +1274,17 @@ try_string_offset: switch (Z_TYPE_P(dim)) { /* case IS_LONG: */ case IS_STRING: - if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, false)) { - break; + if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, false)) { + goto out; } ZVAL_NULL(result); return; + case IS_DOUBLE: + offset = zend_dval_to_lval_silent(Z_DVAL_P(dim)); + goto out; case IS_UNDEF: zend_jit_undefined_op_helper(EG(current_execute_data)->opline->op2.var); - case IS_DOUBLE: + ZEND_FALLTHROUGH; case IS_NULL: case IS_FALSE: case IS_TRUE: @@ -1314,6 +1305,7 @@ try_string_offset: offset = Z_LVAL_P(dim); } +out: if ((zend_ulong)offset >= (zend_ulong)ZSTR_LEN(str)) { if (offset < 0) { /* Handle negative offset */ diff --git a/ext/opcache/tests/jit/add_011.phpt b/ext/opcache/tests/jit/add_011.phpt index 28c598c5540..17bd7be99d1 100644 --- a/ext/opcache/tests/jit/add_011.phpt +++ b/ext/opcache/tests/jit/add_011.phpt @@ -195,5 +195,6 @@ int(-9223371969208523780) Warning: Undefined variable $u in %sadd_011.php on line 5 -Deprecated: Implicit conversion from float %f to int loses precision in %sadd_011.php on line 5 -int(66572500992) \ No newline at end of file +Warning: The float %f is not representable as an int, cast occurred in %sadd_011.php on line 5 +int(66572500992) + diff --git a/ext/opcache/tests/jit/array_elem_002.phpt b/ext/opcache/tests/jit/array_elem_002.phpt index 250f5434905..01de92acfcc 100644 --- a/ext/opcache/tests/jit/array_elem_002.phpt +++ b/ext/opcache/tests/jit/array_elem_002.phpt @@ -11,7 +11,7 @@ $string_float= PHP_INT_MAX; $a = [$float => 'a', $string_float => 'b', 'c', 'd']; ?> --EXPECTF-- -Deprecated: Implicit conversion from float 1.0E+38 to int loses precision in %sarray_elem_002.php on line 4 +Warning: The float 1.0E+38 is not representable as an int, cast occurred in %sarray_elem_002.php on line 4 Fatal error: Uncaught Error: Cannot add element to the array as the next element is already occupied in %sarray_elem_002.php:4 Stack trace: diff --git a/ext/opcache/tests/jit/gh19669-001.phpt b/ext/opcache/tests/jit/gh19669-001.phpt index 7d63643bb01..e8c54c542c8 100644 --- a/ext/opcache/tests/jit/gh19669-001.phpt +++ b/ext/opcache/tests/jit/gh19669-001.phpt @@ -20,5 +20,6 @@ function test() { } var_dump(test()); ?> ---EXPECT-- +--EXPECTF-- +Warning: The float -1.8446744073709552E+19 is not representable as an int, cast occurred in %s on line %d int(-3) diff --git a/ext/opcache/tests/jit/gh19669-002.phpt b/ext/opcache/tests/jit/gh19669-002.phpt index 373356bcd06..3a1aa0aa958 100644 --- a/ext/opcache/tests/jit/gh19669-002.phpt +++ b/ext/opcache/tests/jit/gh19669-002.phpt @@ -20,5 +20,6 @@ function test() { } var_dump(test()); ?> ---EXPECT-- +--EXPECTF-- +Warning: The float -1.8446744073709552E+19 is not representable as an int, cast occurred in %s on line %d int(-10) diff --git a/ext/opcache/tests/jit/reg_alloc_003_32bits.phpt b/ext/opcache/tests/jit/reg_alloc_003_32bits.phpt index 9b373c62308..ecef070c3f2 100644 --- a/ext/opcache/tests/jit/reg_alloc_003_32bits.phpt +++ b/ext/opcache/tests/jit/reg_alloc_003_32bits.phpt @@ -23,5 +23,5 @@ function test($char_code) { echo test(65), "\n"; ?> --EXPECTF-- -Deprecated: Implicit conversion from float 4294967168 to int loses precision in %s on line %d +Warning: The float 4294967168 is not representable as an int, cast occurred in %s on line %d correct diff --git a/ext/openssl/tests/openssl_decrypt_basic.phpt b/ext/openssl/tests/openssl_decrypt_basic.phpt index 6cb2297a979..0f674dbc1e3 100644 --- a/ext/openssl/tests/openssl_decrypt_basic.phpt +++ b/ext/openssl/tests/openssl_decrypt_basic.phpt @@ -10,8 +10,7 @@ $password = "openssl"; $ivlen = openssl_cipher_iv_length($method); $iv = ''; -srand(time() + ((int)(microtime(true) * 1000000) % 1000000)); -while(strlen($iv) < $ivlen) $iv .= chr(rand(0,255)); +while(strlen($iv) < $ivlen) $iv .= random_bytes(1); $encrypted = openssl_encrypt($data, $method, $password, 0, $iv); $output = openssl_decrypt($encrypted, $method, $password, 0, $iv); diff --git a/ext/standard/array.c b/ext/standard/array.c index 96795e623cb..be9702c5ef6 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1219,7 +1219,7 @@ PHP_FUNCTION(min) min_lval = Z_LVAL(args[i]); min = &args[i]; } - } else if (Z_TYPE(args[i]) == IS_DOUBLE && (zend_dval_to_lval((double) min_lval) == min_lval)) { + } else if (Z_TYPE(args[i]) == IS_DOUBLE && (zend_dval_to_lval_silent((double) min_lval) == min_lval)) { /* if min_lval can be exactly represented as a double, go to double dedicated code */ min_dval = (double) min_lval; goto double_compare; @@ -1239,7 +1239,7 @@ PHP_FUNCTION(min) min_dval = Z_DVAL(args[i]); min = &args[i]; } - } else if (Z_TYPE(args[i]) == IS_LONG && (zend_dval_to_lval((double) Z_LVAL(args[i])) == Z_LVAL(args[i]))) { + } else if (Z_TYPE(args[i]) == IS_LONG && (zend_dval_to_lval_silent((double) Z_LVAL(args[i])) == Z_LVAL(args[i]))) { /* if the value can be exactly represented as a double, use double dedicated code otherwise generic */ if (min_dval > (double)Z_LVAL(args[i])) { min_dval = (double)Z_LVAL(args[i]); @@ -1277,7 +1277,7 @@ ZEND_FRAMELESS_FUNCTION(min, 2) if (EXPECTED(Z_TYPE_P(rhs) == IS_LONG)) { RETURN_COPY_VALUE(lhs_lval < Z_LVAL_P(rhs) ? lhs : rhs); - } else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval((double) lhs_lval) == lhs_lval)) { + } else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval_silent((double) lhs_lval) == lhs_lval)) { /* if lhs_lval can be exactly represented as a double, go to double dedicated code */ lhs_dval = (double) lhs_lval; goto double_compare; @@ -1290,7 +1290,7 @@ ZEND_FRAMELESS_FUNCTION(min, 2) if (EXPECTED(Z_TYPE_P(rhs) == IS_DOUBLE)) { double_compare: RETURN_COPY_VALUE(lhs_dval < Z_DVAL_P(rhs) ? lhs : rhs); - } else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) { + } else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval_silent((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) { /* if the value can be exactly represented as a double, use double dedicated code otherwise generic */ RETURN_COPY_VALUE(lhs_dval < (double)Z_LVAL_P(rhs) ? lhs : rhs); } else { @@ -1347,7 +1347,7 @@ PHP_FUNCTION(max) max_lval = Z_LVAL(args[i]); max = &args[i]; } - } else if (Z_TYPE(args[i]) == IS_DOUBLE && (zend_dval_to_lval((double) max_lval) == max_lval)) { + } else if (Z_TYPE(args[i]) == IS_DOUBLE && (zend_dval_to_lval_silent((double) max_lval) == max_lval)) { /* if max_lval can be exactly represented as a double, go to double dedicated code */ max_dval = (double) max_lval; goto double_compare; @@ -1367,7 +1367,7 @@ PHP_FUNCTION(max) max_dval = Z_DVAL(args[i]); max = &args[i]; } - } else if (Z_TYPE(args[i]) == IS_LONG && (zend_dval_to_lval((double) Z_LVAL(args[i])) == Z_LVAL(args[i]))) { + } else if (Z_TYPE(args[i]) == IS_LONG && (zend_dval_to_lval_silent((double) Z_LVAL(args[i])) == Z_LVAL(args[i]))) { /* if the value can be exactly represented as a double, use double dedicated code otherwise generic */ if (max_dval < (double)Z_LVAL(args[i])) { max_dval = (double)Z_LVAL(args[i]); @@ -1405,7 +1405,7 @@ ZEND_FRAMELESS_FUNCTION(max, 2) if (EXPECTED(Z_TYPE_P(rhs) == IS_LONG)) { RETURN_COPY_VALUE(lhs_lval >= Z_LVAL_P(rhs) ? lhs : rhs); - } else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval((double) lhs_lval) == lhs_lval)) { + } else if (Z_TYPE_P(rhs) == IS_DOUBLE && (zend_dval_to_lval_silent((double) lhs_lval) == lhs_lval)) { /* if lhs_lval can be exactly represented as a double, go to double dedicated code */ lhs_dval = (double) lhs_lval; goto double_compare; @@ -1418,7 +1418,7 @@ ZEND_FRAMELESS_FUNCTION(max, 2) if (EXPECTED(Z_TYPE_P(rhs) == IS_DOUBLE)) { double_compare: RETURN_COPY_VALUE(lhs_dval >= Z_DVAL_P(rhs) ? lhs : rhs); - } else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) { + } else if (Z_TYPE_P(rhs) == IS_LONG && (zend_dval_to_lval_silent((double) Z_LVAL_P(rhs)) == Z_LVAL_P(rhs))) { /* if the value can be exactly represented as a double, use double dedicated code otherwise generic */ RETURN_COPY_VALUE(lhs_dval >= (double)Z_LVAL_P(rhs) ? lhs : rhs); } else { @@ -2980,7 +2980,7 @@ PHP_FUNCTION(range) is_step_negative = true; step_double *= -1; } - step = zend_dval_to_lval(step_double); + step = zend_dval_to_lval_silent(step_double); if (!zend_is_long_compatible(step_double, step)) { is_step_double = true; } diff --git a/ext/standard/tests/general_functions/gettype_settype_variation2.phpt b/ext/standard/tests/general_functions/gettype_settype_variation2.phpt index 12f001e924e..5f35d713d51 100644 --- a/ext/standard/tests/general_functions/gettype_settype_variation2.phpt +++ b/ext/standard/tests/general_functions/gettype_settype_variation2.phpt @@ -274,6 +274,7 @@ int(1) string(7) "integer" -- Iteration 20 -- string(6) "string" +2: The float-string "2974394749328742328432" is not representable as an int, cast occurred bool(true) int(2147483647) string(7) "integer" @@ -304,6 +305,7 @@ int(1) string(7) "integer" -- Iteration 26 -- string(6) "string" +2: The float-string "2974394749328742328432" is not representable as an int, cast occurred bool(true) int(2147483647) string(7) "integer" @@ -424,11 +426,13 @@ int(2147483647) string(7) "integer" -- Iteration 50 -- string(6) "double" +2: The float 2147483649 is not representable as an int, cast occurred bool(true) int(-2147483647) string(7) "integer" -- Iteration 51 -- string(6) "double" +2: The float 1232147483649 is not representable as an int, cast occurred bool(true) int(-508130303) string(7) "integer" @@ -439,6 +443,7 @@ int(85) string(7) "integer" -- Iteration 53 -- string(6) "double" +2: The float 1058513956921 is not representable as an int, cast occurred bool(true) int(1952002105) string(7) "integer" @@ -459,6 +464,7 @@ int(-365) string(7) "integer" -- Iteration 57 -- string(6) "double" +2: The float 80561044571754 is not representable as an int, cast occurred bool(true) int(343000682) string(7) "integer" @@ -669,6 +675,7 @@ int(1) string(7) "integer" -- Iteration 20 -- string(6) "string" +2: The float-string "2974394749328742328432" is not representable as an int, cast occurred bool(true) int(2147483647) string(7) "integer" @@ -699,6 +706,7 @@ int(1) string(7) "integer" -- Iteration 26 -- string(6) "string" +2: The float-string "2974394749328742328432" is not representable as an int, cast occurred bool(true) int(2147483647) string(7) "integer" @@ -819,11 +827,13 @@ int(2147483647) string(7) "integer" -- Iteration 50 -- string(6) "double" +2: The float 2147483649 is not representable as an int, cast occurred bool(true) int(-2147483647) string(7) "integer" -- Iteration 51 -- string(6) "double" +2: The float 1232147483649 is not representable as an int, cast occurred bool(true) int(-508130303) string(7) "integer" @@ -834,6 +844,7 @@ int(85) string(7) "integer" -- Iteration 53 -- string(6) "double" +2: The float 1058513956921 is not representable as an int, cast occurred bool(true) int(1952002105) string(7) "integer" @@ -854,6 +865,7 @@ int(-365) string(7) "integer" -- Iteration 57 -- string(6) "double" +2: The float 80561044571754 is not representable as an int, cast occurred bool(true) int(343000682) string(7) "integer" diff --git a/ext/standard/tests/general_functions/intval.phpt b/ext/standard/tests/general_functions/intval.phpt index 51d3b128f47..83377d85a09 100644 --- a/ext/standard/tests/general_functions/intval.phpt +++ b/ext/standard/tests/general_functions/intval.phpt @@ -94,13 +94,10 @@ $not_int_types = array ( array(), array(0), array(1), - array(NULL), array(null), array("string"), array(true), - array(TRUE), array(false), - array(FALSE), array(1,2,3,4), array(1 => "One", "two" => 2), @@ -127,12 +124,6 @@ $not_int_types = array ( /* booleans */ true, false, - TRUE, - FALSE, - - /* undefined and unset vars */ - @$unset_var, - @$undefined_var ); @@ -230,11 +221,19 @@ int(-2147483648) int(2147483647) *** Testing intval() on non integer types *** + +Warning: The float-string "-2147483649" is not representable as an int, cast occurred in %s on line %d int(-2147483648) + +Warning: The float-string "2147483648" is not representable as an int, cast occurred in %s on line %d int(2147483647) int(0) int(0) + +Warning: The float-string "020000000001" is not representable as an int, cast occurred in %s on line %d int(2147483647) + +Warning: The float-string "-020000000001" is not representable as an int, cast occurred in %s on line %d int(-2147483648) int(0) int(0) @@ -256,9 +255,6 @@ int(1) int(1) int(1) int(1) -int(1) -int(1) -int(1) int(0) int(0) int(0) @@ -279,9 +275,5 @@ int(0) int(0) int(1) int(0) -int(1) -int(0) -int(0) -int(0) --- Done --- diff --git a/ext/standard/tests/math/bug30695.phpt b/ext/standard/tests/math/bug30695.phpt index 82771d0e0ab..a2ac79c1126 100644 --- a/ext/standard/tests/math/bug30695.phpt +++ b/ext/standard/tests/math/bug30695.phpt @@ -53,22 +53,22 @@ if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only"); echo "\n", toUTF8(65), "\n", toUTF8(233), "\n", toUTF8(1252), "\n", toUTF8(20095), "\n"; ?> --EXPECTF-- -Deprecated: Implicit conversion from float 4294967168 to int loses precision in %s on line %d +Warning: The float 4294967168 is not representable as an int, cast occurred in %s on line %d A -Deprecated: Implicit conversion from float 4294967168 to int loses precision in %s on line %d +Warning: The float 4294967168 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 4294965248 to int loses precision in %s on line %d +Warning: The float 4294965248 is not representable as an int, cast occurred in %s on line %d é -Deprecated: Implicit conversion from float 4294967168 to int loses precision in %s on line %d +Warning: The float 4294967168 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 4294965248 to int loses precision in %s on line %d +Warning: The float 4294965248 is not representable as an int, cast occurred in %s on line %d Ӥ -Deprecated: Implicit conversion from float 4294967168 to int loses precision in %s on line %d +Warning: The float 4294967168 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 4294965248 to int loses precision in %s on line %d +Warning: The float 4294965248 is not representable as an int, cast occurred in %s on line %d -Deprecated: Implicit conversion from float 4294901760 to int loses precision in %s on line %d +Warning: The float 4294901760 is not representable as an int, cast occurred in %s on line %d 乿 diff --git a/ext/standard/tests/strings/bug47842.phpt b/ext/standard/tests/strings/bug47842.phpt index fe97308ea53..1c8da881e7c 100644 --- a/ext/standard/tests/strings/bug47842.phpt +++ b/ext/standard/tests/strings/bug47842.phpt @@ -23,12 +23,14 @@ printf("printf 64-bit signed int '18446744073709551615' (2^64)-1 = %u\n", 184467 echo "Done\n"; ?> ---EXPECT-- +--EXPECTF-- -Test sscanf 32-bit signed int '2147483647' (2^31)-1 = 2147483647 sscanf 32-bit unsign int '4294967295' (2^32)-1 = 4294967295 sscanf 64-bit signed int '9223372036854775807' (2^63)-1 = 9223372036854775807 sscanf 64-bit unsign int '18446744073709551615' (2^64)-1 = 18446744073709551615 printf 64-bit signed int '9223372036854775807' (2^63)-1 = 9223372036854775807 + +Warning: The float 1.8446744073709552E+19 is not representable as an int, cast occurred in %s on line %d printf 64-bit signed int '18446744073709551615' (2^64)-1 = 0 Done diff --git a/ext/standard/tests/strings/pack.phpt b/ext/standard/tests/strings/pack.phpt index af449266671..e71f9ce37a9 100644 --- a/ext/standard/tests/strings/pack.phpt +++ b/ext/standard/tests/strings/pack.phpt @@ -97,7 +97,7 @@ print_r(unpack("v", pack("v", -1000))); print_r(unpack("v", pack("v", -64434))); print_r(unpack("v", pack("v", -65535))); ?> ---EXPECT-- +--EXPECTF-- Array ( [1] => h @@ -143,10 +143,14 @@ Array ( [1] => -64434 ) + +Warning: The float 4294967296 is not representable as an int, cast occurred in %s on line %d Array ( [1] => 0 ) + +Warning: The float -4294967296 is not representable as an int, cast occurred in %s on line %d Array ( [1] => 0 @@ -159,10 +163,14 @@ Array ( [1] => 0 ) + +Warning: The float 2147483650 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -2147483646 ) + +Warning: The float 4294967295 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -1 @@ -179,10 +187,14 @@ Array ( [1] => 0 ) + +Warning: The float 2147483650 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -2147483646 ) + +Warning: The float 4294967296 is not representable as an int, cast occurred in %s on line %d Array ( [1] => 0 @@ -227,10 +239,14 @@ Array ( [1] => 0 ) + +Warning: The float 2147483650 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -2147483646 ) + +Warning: The float 4294967296 is not representable as an int, cast occurred in %s on line %d Array ( [1] => 0 @@ -299,10 +315,14 @@ Array ( [1] => 0 ) + +Warning: The float 2147483650 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -2147483646 ) + +Warning: The float 4294967296 is not representable as an int, cast occurred in %s on line %d Array ( [1] => 0 diff --git a/ext/standard/tests/strings/pack64.phpt b/ext/standard/tests/strings/pack64.phpt index 84e69008284..238195287b2 100644 --- a/ext/standard/tests/strings/pack64.phpt +++ b/ext/standard/tests/strings/pack64.phpt @@ -37,7 +37,7 @@ print_r(unpack("i", pack("i", -2147483647))); print_r(unpack("i", pack("i", -2147483648))); // Min int32 print_r(unpack("I", pack("I", 4294967295))); // Max uint32 ?> ---EXPECT-- +--EXPECTF-- Array ( [1] => 281474976710654 @@ -46,6 +46,8 @@ Array ( [1] => 0 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -54,6 +56,8 @@ Array ( [1] => -1 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -66,6 +70,8 @@ Array ( [1] => 0 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -74,6 +80,8 @@ Array ( [1] => -1 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -86,6 +94,8 @@ Array ( [1] => 0 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -94,6 +104,8 @@ Array ( [1] => -1 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -106,6 +118,8 @@ Array ( [1] => 0 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 @@ -114,6 +128,8 @@ Array ( [1] => -1 ) + +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d Array ( [1] => -9223372036854775808 diff --git a/ext/standard/tests/strings/vprintf_variation12.phpt b/ext/standard/tests/strings/vprintf_variation12.phpt index cac53da18f4..f2e4270af31 100644 --- a/ext/standard/tests/strings/vprintf_variation12.phpt +++ b/ext/standard/tests/strings/vprintf_variation12.phpt @@ -74,10 +74,16 @@ foreach($args_array as $args) { } ?> ---EXPECT-- +--EXPECTF-- *** Testing vprintf() : octal formats and non-octal values *** -- Iteration 1 -- + +Warning: The float 20000000000 is not representable as an int, cast occurred in %s on line %d + +Warning: The float 2000000000000 is not representable as an int, cast occurred in %s on line %d + +Warning: The float 22000000000000 is not representable as an int, cast occurred in %s on line %d 2 0 12 361100 37777775456 2322 diff --git a/ext/standard/tests/strings/vprintf_variation14.phpt b/ext/standard/tests/strings/vprintf_variation14.phpt index ce65e8726d8..4bda9591777 100644 --- a/ext/standard/tests/strings/vprintf_variation14.phpt +++ b/ext/standard/tests/strings/vprintf_variation14.phpt @@ -75,10 +75,16 @@ foreach($args_array as $args) { } ?> ---EXPECT-- +--EXPECTF-- *** Testing vprintf() : hexa formats and non-hexa values *** -- Iteration 1 -- + +Warning: The float 20000000000 is not representable as an int, cast occurred in %s on line %d + +Warning: The float 2000000000000 is not representable as an int, cast occurred in %s on line %d + +Warning: The float 22000000000000 is not representable as an int, cast occurred in %s on line %d 2 0 a 1e240 x fffffb2e 4d2 diff --git a/ext/standard/tests/strings/vprintf_variation15.phpt b/ext/standard/tests/strings/vprintf_variation15.phpt index c8ae74f2aa7..2eda443ebb9 100644 --- a/ext/standard/tests/strings/vprintf_variation15.phpt +++ b/ext/standard/tests/strings/vprintf_variation15.phpt @@ -44,7 +44,7 @@ foreach($formats as $format) { } ?> ---EXPECT-- +--EXPECTF-- *** Testing vprintf() : unsigned formats and unsigned values *** -- Iteration 1 -- @@ -52,10 +52,14 @@ foreach($formats as $format) { int(16) -- Iteration 2 -- + +Warning: The float 12345678900 is not representable as an int, cast occurred in %s on line %d 3755744308 1234 12345 int(21) -- Iteration 3 -- + +Warning: The float 101234567000 is not representable as an int, cast occurred in %s on line %d 1234000 2450319192 120 int(25) diff --git a/ext/standard/tests/strings/vprintf_variation15_64bit.phpt b/ext/standard/tests/strings/vprintf_variation15_64bit.phpt index 2729e8f54af..0ccb490095a 100644 --- a/ext/standard/tests/strings/vprintf_variation15_64bit.phpt +++ b/ext/standard/tests/strings/vprintf_variation15_64bit.phpt @@ -44,7 +44,7 @@ foreach($formats as $format) { } ?> ---EXPECT-- +--EXPECTF-- *** Testing vprintf() : unsigned formats and unsigned values *** -- Iteration 1 -- @@ -56,6 +56,8 @@ int(16) int(22) -- Iteration 3 -- + +Warning: The float 1.0E+21 is not representable as an int, cast occurred in %s on line %d 1234000 3875820019684212736 120 int(34) diff --git a/ext/standard/tests/strings/vprintf_variation16.phpt b/ext/standard/tests/strings/vprintf_variation16.phpt index 70ecdab99e0..c064b7c4f0c 100644 --- a/ext/standard/tests/strings/vprintf_variation16.phpt +++ b/ext/standard/tests/strings/vprintf_variation16.phpt @@ -66,10 +66,16 @@ foreach($args_array as $args) { $counter++; } ?> ---EXPECT-- +--EXPECTF-- *** Testing vprintf() : unsigned formats and signed & other types of values *** -- Iteration 1 -- + +Warning: The float 20000000000 is not representable as an int, cast occurred in %s on line %d + +Warning: The float 2000000000000 is not representable as an int, cast occurred in %s on line %d + +Warning: The float 22000000000000 is not representable as an int, cast occurred in %s on line %d 2 0 10 123456 123456 1234 2820130816 2840207360 1177509888 diff --git a/ext/standard/tests/strings/vprintf_variation4.phpt b/ext/standard/tests/strings/vprintf_variation4.phpt index 63018a9db8a..96836f83ee6 100644 --- a/ext/standard/tests/strings/vprintf_variation4.phpt +++ b/ext/standard/tests/strings/vprintf_variation4.phpt @@ -67,10 +67,12 @@ foreach($args_array as $args) { } ?> ---EXPECT-- +--EXPECTF-- *** Testing vprintf() : int formats and non-integer values *** -- Iteration 1 -- + +Warning: The float 20000000000 is not representable as an int, cast occurred in %s on line %d 2 +0 10 123456 -1234 1234 -1474836480 200000 4000 22000000 diff --git a/main/php_variables.c b/main/php_variables.c index e0fe979d94d..971e1c77ea9 100644 --- a/main/php_variables.c +++ b/main/php_variables.c @@ -745,7 +745,7 @@ static inline void php_register_server_variables(void) /* store request init time */ ZVAL_DOUBLE(&tmp, sapi_get_request_time()); php_register_variable_quick("REQUEST_TIME_FLOAT", sizeof("REQUEST_TIME_FLOAT")-1, &tmp, ht); - ZVAL_LONG(&tmp, zend_dval_to_lval(Z_DVAL(tmp))); + ZVAL_LONG(&tmp, zend_dval_to_lval_silent(Z_DVAL(tmp))); php_register_variable_quick("REQUEST_TIME", sizeof("REQUEST_TIME")-1, &tmp, ht); } /* }}} */ diff --git a/tests/lang/bug27354.phpt b/tests/lang/bug27354.phpt index 5d18910bc9f..e3cac1b1702 100644 --- a/tests/lang/bug27354.phpt +++ b/tests/lang/bug27354.phpt @@ -10,7 +10,7 @@ var_dump(-2147483648 % -2); --EXPECTF-- int(0) -Deprecated: Implicit conversion from float -9.223372036860776E+18 to int loses precision in %s on line %d +Warning: The float -9.223372036860776E+18 is not representable as an int, cast occurred in %s on line %d int(0) int(0) int(0) diff --git a/tests/lang/operators/bitwiseNot_basiclong_64bit.phpt b/tests/lang/operators/bitwiseNot_basiclong_64bit.phpt index 0e701051f5f..1a1105a557a 100644 --- a/tests/lang/operators/bitwiseNot_basiclong_64bit.phpt +++ b/tests/lang/operators/bitwiseNot_basiclong_64bit.phpt @@ -52,7 +52,7 @@ int(-4294967294) int(-9223372036854775807) --- testing: 9.2233720368548E+18 --- -Deprecated: Implicit conversion from float 9.223372036854776E+18 to int loses precision in %s on line %d +Warning: The float 9.223372036854776E+18 is not representable as an int, cast occurred in %s on line %d int(9223372036854775807) --- testing: -9223372036854775807 --- int(9223372036854775806)