Convert Division by 0 warnings to Error per the Engine Warning RFC

Closes GH-6125
This commit is contained in:
George Peter Banyard 2020-09-13 00:43:00 +02:00
parent bf515649ff
commit 31ef94cd42
6 changed files with 69 additions and 64 deletions

View File

@ -10,11 +10,15 @@ $foo = -sin(0);
var_dump($foo);
var_dump(@(1.0 / -0.0));
try {
var_dump(1.0 / -0.0);
} catch (\DivisionByZeroError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
float(-0)
float(-0)
float(-0)
float(-INF)
Division by zero

View File

@ -60,23 +60,26 @@ try {
}
?>
--EXPECTF--
Warning: Division by zero in %sbug69957.php on line %d
float(INF)
--EXPECT--
Variable div
Type: DivisionByZeroError
Message: Division by zero
Variable mod
Type: DivisionByZeroError
Message: Modulo by zero
Warning: Division by zero in %sbug69957.php on line %d
float(INF)
Literal div
Type: DivisionByZeroError
Message: Division by zero
Literal mod
Type: DivisionByZeroError
Message: Modulo by zero
Warning: Division by zero in %sbug69957.php on line %d
float(INF)
Double div
Type: DivisionByZeroError
Message: Division by zero
Double mod
Type: DivisionByZeroError

View File

@ -16,23 +16,14 @@ class T {
}
$x = new T;
$x->x = 1;
try {
$x->x = 1;
} catch (\DivisionByZeroError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECTF--
Warning: Undefined variable $undefined in %s on line %d
Warning: Attempt to read property "1" on null in %s on line %d
Warning: Division by zero in %s on line %d
Warning: Undefined variable $undefined in %s on line %d
Warning: Attempt to read property "NAN" on null in %s on line %d
Warning: Division by zero in %s on line %d
Warning: Undefined variable $undefined in %s on line %d
Warning: Attempt to read property "NAN" on null in %s on line %d
Warning: Division by zero in %s on line %d
Division by zero

View File

@ -1253,18 +1253,13 @@ ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *o
}
/* }}} */
#ifdef __clang__
__attribute__((no_sanitize("float-divide-by-zero")))
#endif
static zend_result ZEND_FASTCALL div_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
{
zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
if (Z_LVAL_P(op2) == 0) {
zend_error(E_WARNING, "Division by zero");
ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1) / (double) Z_LVAL_P(op2)));
return SUCCESS;
goto division_by_0;
} else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
/* Prevent overflow error/crash */
ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
@ -1278,25 +1273,31 @@ static zend_result ZEND_FASTCALL div_function_base(zval *result, zval *op1, zval
return SUCCESS;
} else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
if (Z_DVAL_P(op2) == 0) {
zend_error(E_WARNING, "Division by zero");
goto division_by_0;
}
ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
return SUCCESS;
} else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
if (Z_LVAL_P(op2) == 0) {
zend_error(E_WARNING, "Division by zero");
goto division_by_0;
}
ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
return SUCCESS;
} else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
if (Z_DVAL_P(op2) == 0) {
zend_error(E_WARNING, "Division by zero");
goto division_by_0;
}
ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
return SUCCESS;
} else {
return FAILURE;
}
division_by_0:
if (result != op1) {
ZVAL_UNDEF(result);
}
zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
return SUCCESS;
}
/* }}} */

View File

@ -23,10 +23,14 @@ $otherVals = array(0, 1, -1, 7, 9, 65, -44, MAX_32Bit, MAX_64Bit);
error_reporting(E_ERROR);
foreach ($longVals as $longVal) {
foreach($otherVals as $otherVal) {
echo "--- testing: $longVal / $otherVal ---\n";
var_dump($longVal/$otherVal);
}
foreach($otherVals as $otherVal) {
echo "--- testing: $longVal / $otherVal ---\n";
try {
var_dump($longVal/$otherVal);
} catch (\Throwable $e) {
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
}
}
}
foreach ($otherVals as $otherVal) {
@ -39,7 +43,7 @@ foreach ($otherVals as $otherVal) {
?>
--EXPECT--
--- testing: 9223372036854775807 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 9223372036854775807 / 1 ---
int(9223372036854775807)
--- testing: 9223372036854775807 / -1 ---
@ -57,7 +61,7 @@ float(4294967298)
--- testing: 9223372036854775807 / 9223372036854775807 ---
int(1)
--- testing: -9223372036854775808 / 0 ---
float(-INF)
DivisionByZeroError: Division by zero
--- testing: -9223372036854775808 / 1 ---
int(-9223372036854775808)
--- testing: -9223372036854775808 / -1 ---
@ -75,7 +79,7 @@ float(-4294967298)
--- testing: -9223372036854775808 / 9223372036854775807 ---
float(-1)
--- testing: 2147483647 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 2147483647 / 1 ---
int(2147483647)
--- testing: 2147483647 / -1 ---
@ -93,7 +97,7 @@ int(1)
--- testing: 2147483647 / 9223372036854775807 ---
float(2.328306435454494E-10)
--- testing: -2147483648 / 0 ---
float(-INF)
DivisionByZeroError: Division by zero
--- testing: -2147483648 / 1 ---
int(-2147483648)
--- testing: -2147483648 / -1 ---
@ -111,7 +115,7 @@ float(-1.0000000004656613)
--- testing: -2147483648 / 9223372036854775807 ---
float(-2.3283064365386963E-10)
--- testing: 9223372034707292160 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 9223372034707292160 / 1 ---
int(9223372034707292160)
--- testing: 9223372034707292160 / -1 ---
@ -129,7 +133,7 @@ float(4294967297)
--- testing: 9223372034707292160 / 9223372036854775807 ---
float(0.9999999997671694)
--- testing: -9223372034707292160 / 0 ---
float(-INF)
DivisionByZeroError: Division by zero
--- testing: -9223372034707292160 / 1 ---
int(-9223372034707292160)
--- testing: -9223372034707292160 / -1 ---
@ -147,7 +151,7 @@ float(-4294967297)
--- testing: -9223372034707292160 / 9223372036854775807 ---
float(-0.9999999997671694)
--- testing: 2147483648 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 2147483648 / 1 ---
int(2147483648)
--- testing: 2147483648 / -1 ---
@ -165,7 +169,7 @@ float(1.0000000004656613)
--- testing: 2147483648 / 9223372036854775807 ---
float(2.3283064365386963E-10)
--- testing: -2147483649 / 0 ---
float(-INF)
DivisionByZeroError: Division by zero
--- testing: -2147483649 / 1 ---
int(-2147483649)
--- testing: -2147483649 / -1 ---
@ -183,7 +187,7 @@ float(-1.0000000009313226)
--- testing: -2147483649 / 9223372036854775807 ---
float(-2.3283064376228985E-10)
--- testing: 4294967294 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 4294967294 / 1 ---
int(4294967294)
--- testing: 4294967294 / -1 ---
@ -201,7 +205,7 @@ int(2)
--- testing: 4294967294 / 9223372036854775807 ---
float(4.656612870908988E-10)
--- testing: 4294967295 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 4294967295 / 1 ---
int(4294967295)
--- testing: 4294967295 / -1 ---
@ -219,7 +223,7 @@ float(2.0000000004656613)
--- testing: 4294967295 / 9223372036854775807 ---
float(4.6566128719931904E-10)
--- testing: 4294967293 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 4294967293 / 1 ---
int(4294967293)
--- testing: 4294967293 / -1 ---
@ -237,7 +241,7 @@ float(1.9999999995343387)
--- testing: 4294967293 / 9223372036854775807 ---
float(4.656612869824786E-10)
--- testing: 9223372036854775806 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 9223372036854775806 / 1 ---
int(9223372036854775806)
--- testing: 9223372036854775806 / -1 ---
@ -255,7 +259,7 @@ int(4294967298)
--- testing: 9223372036854775806 / 9223372036854775807 ---
float(1)
--- testing: 9.2233720368548E+18 / 0 ---
float(INF)
DivisionByZeroError: Division by zero
--- testing: 9.2233720368548E+18 / 1 ---
float(9.223372036854776E+18)
--- testing: 9.2233720368548E+18 / -1 ---
@ -273,7 +277,7 @@ float(4294967298)
--- testing: 9.2233720368548E+18 / 9223372036854775807 ---
float(1)
--- testing: -9223372036854775807 / 0 ---
float(-INF)
DivisionByZeroError: Division by zero
--- testing: -9223372036854775807 / 1 ---
int(-9223372036854775807)
--- testing: -9223372036854775807 / -1 ---
@ -291,7 +295,7 @@ float(-4294967298)
--- testing: -9223372036854775807 / 9223372036854775807 ---
int(-1)
--- testing: -9.2233720368548E+18 / 0 ---
float(-INF)
DivisionByZeroError: Division by zero
--- testing: -9.2233720368548E+18 / 1 ---
float(-9.223372036854776E+18)
--- testing: -9.2233720368548E+18 / -1 ---

View File

@ -17,6 +17,8 @@ foreach ($strVals as $strVal) {
var_dump($strVal/$otherVal);
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
} catch (\DivisionByZeroError $e) {
echo $e->getMessage() . \PHP_EOL;
}
}
}
@ -25,7 +27,7 @@ foreach ($strVals as $strVal) {
?>
--EXPECT--
--- testing: '0'/'0' ---
float(NAN)
Division by zero
--- testing: '0'/'65' ---
int(0)
--- testing: '0'/'-44' ---
@ -53,7 +55,7 @@ float(0)
--- testing: '0'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '65'/'0' ---
float(INF)
Division by zero
--- testing: '65'/'65' ---
int(1)
--- testing: '65'/'-44' ---
@ -81,7 +83,7 @@ float(19.11764705882353)
--- testing: '65'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '-44'/'0' ---
float(-INF)
Division by zero
--- testing: '-44'/'65' ---
float(-0.676923076923077)
--- testing: '-44'/'-44' ---
@ -109,7 +111,7 @@ float(-12.941176470588236)
--- testing: '-44'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '1.2'/'0' ---
float(INF)
Division by zero
--- testing: '1.2'/'65' ---
float(0.01846153846153846)
--- testing: '1.2'/'-44' ---
@ -137,7 +139,7 @@ float(0.35294117647058826)
--- testing: '1.2'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '-7.7'/'0' ---
float(-INF)
Division by zero
--- testing: '-7.7'/'65' ---
float(-0.11846153846153847)
--- testing: '-7.7'/'-44' ---
@ -193,7 +195,7 @@ Unsupported operand types: string / string
--- testing: 'abc'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '123abc'/'0' ---
float(INF)
Division by zero
--- testing: '123abc'/'65' ---
float(1.8923076923076922)
--- testing: '123abc'/'-44' ---
@ -221,7 +223,7 @@ float(36.1764705882353)
--- testing: '123abc'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '123e5'/'0' ---
float(INF)
Division by zero
--- testing: '123e5'/'65' ---
float(189230.76923076922)
--- testing: '123e5'/'-44' ---
@ -249,7 +251,7 @@ float(3617647.0588235296)
--- testing: '123e5'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '123e5xyz'/'0' ---
float(INF)
Division by zero
--- testing: '123e5xyz'/'65' ---
float(189230.76923076922)
--- testing: '123e5xyz'/'-44' ---
@ -277,7 +279,7 @@ float(3617647.0588235296)
--- testing: '123e5xyz'/'a5.9' ---
Unsupported operand types: string / string
--- testing: ' 123abc'/'0' ---
float(INF)
Division by zero
--- testing: ' 123abc'/'65' ---
float(1.8923076923076922)
--- testing: ' 123abc'/'-44' ---
@ -305,7 +307,7 @@ float(36.1764705882353)
--- testing: ' 123abc'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '123 abc'/'0' ---
float(INF)
Division by zero
--- testing: '123 abc'/'65' ---
float(1.8923076923076922)
--- testing: '123 abc'/'-44' ---
@ -333,7 +335,7 @@ float(36.1764705882353)
--- testing: '123 abc'/'a5.9' ---
Unsupported operand types: string / string
--- testing: '123abc '/'0' ---
float(INF)
Division by zero
--- testing: '123abc '/'65' ---
float(1.8923076923076922)
--- testing: '123abc '/'-44' ---
@ -361,7 +363,7 @@ float(36.1764705882353)
--- testing: '123abc '/'a5.9' ---
Unsupported operand types: string / string
--- testing: '3.4a'/'0' ---
float(INF)
Division by zero
--- testing: '3.4a'/'65' ---
float(0.052307692307692305)
--- testing: '3.4a'/'-44' ---