Skip to content

Commit e030f6a

Browse files
committed
Convert Division by 0 warnings to Error per the Engine Warning RFC
1 parent 67d21bf commit e030f6a

File tree

6 files changed

+68
-65
lines changed

6 files changed

+68
-65
lines changed

Zend/tests/bug52355.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ $foo = -sin(0);
1010

1111
var_dump($foo);
1212

13-
var_dump(@(1.0 / -0.0));
13+
try {
14+
var_dump(1.0 / -0.0);
15+
} catch (\DivisionByZeroError $e) {
16+
echo $e->getMessage() . \PHP_EOL;
17+
}
1418

1519
?>
1620
--EXPECT--
1721
float(-0)
1822
float(-0)
1923
float(-0)
20-
float(-INF)
24+
Division by zero

Zend/tests/bug69957.phpt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,26 @@ try {
6060
}
6161

6262
?>
63-
--EXPECTF--
64-
Warning: Division by zero in %sbug69957.php on line %d
65-
float(INF)
63+
--EXPECT--
64+
Variable div
65+
Type: DivisionByZeroError
66+
Message: Division by zero
6667

6768
Variable mod
6869
Type: DivisionByZeroError
6970
Message: Modulo by zero
7071

71-
Warning: Division by zero in %sbug69957.php on line %d
72-
float(INF)
72+
Literal div
73+
Type: DivisionByZeroError
74+
Message: Division by zero
7375

7476
Literal mod
7577
Type: DivisionByZeroError
7678
Message: Modulo by zero
7779

78-
Warning: Division by zero in %sbug69957.php on line %d
79-
float(INF)
80+
Double div
81+
Type: DivisionByZeroError
82+
Message: Division by zero
8083

8184
Double mod
8285
Type: DivisionByZeroError

Zend/tests/bug76667.phpt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,14 @@ class T {
1616
}
1717

1818
$x = new T;
19-
$x->x = 1;
19+
try {
20+
$x->x = 1;
21+
} catch (\DivisionByZeroError $e) {
22+
echo $e->getMessage() . \PHP_EOL;
23+
}
2024
?>
2125
--EXPECTF--
2226
Warning: Undefined variable $undefined in %s on line %d
2327

2428
Warning: Attempt to read property "1" on null in %s on line %d
25-
26-
Warning: Division by zero in %s on line %d
27-
28-
Warning: Undefined variable $undefined in %s on line %d
29-
30-
Warning: Attempt to read property "NAN" on null in %s on line %d
31-
32-
Warning: Division by zero in %s on line %d
33-
34-
Warning: Undefined variable $undefined in %s on line %d
35-
36-
Warning: Attempt to read property "NAN" on null in %s on line %d
37-
38-
Warning: Division by zero in %s on line %d
29+
Division by zero

Zend/zend_operators.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,18 +1253,14 @@ ZEND_API zend_result ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *o
12531253
}
12541254
/* }}} */
12551255

1256-
#ifdef __clang__
1257-
__attribute__((no_sanitize("float-divide-by-zero")))
1258-
#endif
12591256
static zend_result ZEND_FASTCALL div_function_base(zval *result, zval *op1, zval *op2) /* {{{ */
12601257
{
12611258
zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
12621259

12631260
if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
12641261
if (Z_LVAL_P(op2) == 0) {
1265-
zend_error(E_WARNING, "Division by zero");
1266-
ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1) / (double) Z_LVAL_P(op2)));
1267-
return SUCCESS;
1262+
zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1263+
return FAILURE;
12681264
} else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
12691265
/* Prevent overflow error/crash */
12701266
ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
@@ -1278,19 +1274,22 @@ static zend_result ZEND_FASTCALL div_function_base(zval *result, zval *op1, zval
12781274
return SUCCESS;
12791275
} else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
12801276
if (Z_DVAL_P(op2) == 0) {
1281-
zend_error(E_WARNING, "Division by zero");
1277+
zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1278+
return FAILURE;
12821279
}
12831280
ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
12841281
return SUCCESS;
12851282
} else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
12861283
if (Z_LVAL_P(op2) == 0) {
1287-
zend_error(E_WARNING, "Division by zero");
1284+
zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1285+
return FAILURE;
12881286
}
12891287
ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
12901288
return SUCCESS;
12911289
} else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
12921290
if (Z_DVAL_P(op2) == 0) {
1293-
zend_error(E_WARNING, "Division by zero");
1291+
zend_throw_error(zend_ce_division_by_zero_error, "Division by zero");
1292+
return FAILURE;
12941293
}
12951294
ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
12961295
return SUCCESS;
@@ -1328,7 +1327,7 @@ ZEND_API zend_result ZEND_FASTCALL div_function(zval *result, zval *op1, zval *o
13281327
return SUCCESS;
13291328
}
13301329

1331-
ZEND_ASSERT(0 && "Operation must succeed");
1330+
ZEND_ASSERT(EG(exception));
13321331
return FAILURE;
13331332
}
13341333
/* }}} */

tests/lang/operators/divide_basiclong_64bit.phpt

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ $otherVals = array(0, 1, -1, 7, 9, 65, -44, MAX_32Bit, MAX_64Bit);
2323
error_reporting(E_ERROR);
2424

2525
foreach ($longVals as $longVal) {
26-
foreach($otherVals as $otherVal) {
27-
echo "--- testing: $longVal / $otherVal ---\n";
28-
var_dump($longVal/$otherVal);
29-
}
26+
foreach($otherVals as $otherVal) {
27+
echo "--- testing: $longVal / $otherVal ---\n";
28+
try {
29+
var_dump($longVal/$otherVal);
30+
} catch (\Throwable $e) {
31+
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
32+
}
33+
}
3034
}
3135

3236
foreach ($otherVals as $otherVal) {
@@ -39,7 +43,7 @@ foreach ($otherVals as $otherVal) {
3943
?>
4044
--EXPECT--
4145
--- testing: 9223372036854775807 / 0 ---
42-
float(INF)
46+
DivisionByZeroError: Division by zero
4347
--- testing: 9223372036854775807 / 1 ---
4448
int(9223372036854775807)
4549
--- testing: 9223372036854775807 / -1 ---
@@ -57,7 +61,7 @@ float(4294967298)
5761
--- testing: 9223372036854775807 / 9223372036854775807 ---
5862
int(1)
5963
--- testing: -9223372036854775808 / 0 ---
60-
float(-INF)
64+
DivisionByZeroError: Division by zero
6165
--- testing: -9223372036854775808 / 1 ---
6266
int(-9223372036854775808)
6367
--- testing: -9223372036854775808 / -1 ---
@@ -75,7 +79,7 @@ float(-4294967298)
7579
--- testing: -9223372036854775808 / 9223372036854775807 ---
7680
float(-1)
7781
--- testing: 2147483647 / 0 ---
78-
float(INF)
82+
DivisionByZeroError: Division by zero
7983
--- testing: 2147483647 / 1 ---
8084
int(2147483647)
8185
--- testing: 2147483647 / -1 ---
@@ -93,7 +97,7 @@ int(1)
9397
--- testing: 2147483647 / 9223372036854775807 ---
9498
float(2.328306435454494E-10)
9599
--- testing: -2147483648 / 0 ---
96-
float(-INF)
100+
DivisionByZeroError: Division by zero
97101
--- testing: -2147483648 / 1 ---
98102
int(-2147483648)
99103
--- testing: -2147483648 / -1 ---
@@ -111,7 +115,7 @@ float(-1.0000000004656613)
111115
--- testing: -2147483648 / 9223372036854775807 ---
112116
float(-2.3283064365386963E-10)
113117
--- testing: 9223372034707292160 / 0 ---
114-
float(INF)
118+
DivisionByZeroError: Division by zero
115119
--- testing: 9223372034707292160 / 1 ---
116120
int(9223372034707292160)
117121
--- testing: 9223372034707292160 / -1 ---
@@ -129,7 +133,7 @@ float(4294967297)
129133
--- testing: 9223372034707292160 / 9223372036854775807 ---
130134
float(0.9999999997671694)
131135
--- testing: -9223372034707292160 / 0 ---
132-
float(-INF)
136+
DivisionByZeroError: Division by zero
133137
--- testing: -9223372034707292160 / 1 ---
134138
int(-9223372034707292160)
135139
--- testing: -9223372034707292160 / -1 ---
@@ -147,7 +151,7 @@ float(-4294967297)
147151
--- testing: -9223372034707292160 / 9223372036854775807 ---
148152
float(-0.9999999997671694)
149153
--- testing: 2147483648 / 0 ---
150-
float(INF)
154+
DivisionByZeroError: Division by zero
151155
--- testing: 2147483648 / 1 ---
152156
int(2147483648)
153157
--- testing: 2147483648 / -1 ---
@@ -165,7 +169,7 @@ float(1.0000000004656613)
165169
--- testing: 2147483648 / 9223372036854775807 ---
166170
float(2.3283064365386963E-10)
167171
--- testing: -2147483649 / 0 ---
168-
float(-INF)
172+
DivisionByZeroError: Division by zero
169173
--- testing: -2147483649 / 1 ---
170174
int(-2147483649)
171175
--- testing: -2147483649 / -1 ---
@@ -183,7 +187,7 @@ float(-1.0000000009313226)
183187
--- testing: -2147483649 / 9223372036854775807 ---
184188
float(-2.3283064376228985E-10)
185189
--- testing: 4294967294 / 0 ---
186-
float(INF)
190+
DivisionByZeroError: Division by zero
187191
--- testing: 4294967294 / 1 ---
188192
int(4294967294)
189193
--- testing: 4294967294 / -1 ---
@@ -201,7 +205,7 @@ int(2)
201205
--- testing: 4294967294 / 9223372036854775807 ---
202206
float(4.656612870908988E-10)
203207
--- testing: 4294967295 / 0 ---
204-
float(INF)
208+
DivisionByZeroError: Division by zero
205209
--- testing: 4294967295 / 1 ---
206210
int(4294967295)
207211
--- testing: 4294967295 / -1 ---
@@ -219,7 +223,7 @@ float(2.0000000004656613)
219223
--- testing: 4294967295 / 9223372036854775807 ---
220224
float(4.6566128719931904E-10)
221225
--- testing: 4294967293 / 0 ---
222-
float(INF)
226+
DivisionByZeroError: Division by zero
223227
--- testing: 4294967293 / 1 ---
224228
int(4294967293)
225229
--- testing: 4294967293 / -1 ---
@@ -237,7 +241,7 @@ float(1.9999999995343387)
237241
--- testing: 4294967293 / 9223372036854775807 ---
238242
float(4.656612869824786E-10)
239243
--- testing: 9223372036854775806 / 0 ---
240-
float(INF)
244+
DivisionByZeroError: Division by zero
241245
--- testing: 9223372036854775806 / 1 ---
242246
int(9223372036854775806)
243247
--- testing: 9223372036854775806 / -1 ---
@@ -255,7 +259,7 @@ int(4294967298)
255259
--- testing: 9223372036854775806 / 9223372036854775807 ---
256260
float(1)
257261
--- testing: 9.2233720368548E+18 / 0 ---
258-
float(INF)
262+
DivisionByZeroError: Division by zero
259263
--- testing: 9.2233720368548E+18 / 1 ---
260264
float(9.223372036854776E+18)
261265
--- testing: 9.2233720368548E+18 / -1 ---
@@ -273,7 +277,7 @@ float(4294967298)
273277
--- testing: 9.2233720368548E+18 / 9223372036854775807 ---
274278
float(1)
275279
--- testing: -9223372036854775807 / 0 ---
276-
float(-INF)
280+
DivisionByZeroError: Division by zero
277281
--- testing: -9223372036854775807 / 1 ---
278282
int(-9223372036854775807)
279283
--- testing: -9223372036854775807 / -1 ---
@@ -291,7 +295,7 @@ float(-4294967298)
291295
--- testing: -9223372036854775807 / 9223372036854775807 ---
292296
int(-1)
293297
--- testing: -9.2233720368548E+18 / 0 ---
294-
float(-INF)
298+
DivisionByZeroError: Division by zero
295299
--- testing: -9.2233720368548E+18 / 1 ---
296300
float(-9.223372036854776E+18)
297301
--- testing: -9.2233720368548E+18 / -1 ---

0 commit comments

Comments
 (0)