Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Zend/tests/bug52355.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 10 additions & 7 deletions Zend/tests/bug69957.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 6 additions & 15 deletions Zend/tests/bug76667.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
/* }}} */

Expand Down
42 changes: 23 additions & 19 deletions tests/lang/operators/divide_basiclong_64bit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand All @@ -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 ---
Expand Down
Loading