Skip to content

Commit 84f2a98

Browse files
committed
Elevate warnings to Error Exceptions in ext/bcmath
`bcdiv()` and `bcmod()` throw DivisionByZeroError if the divisor is 0, which matches the behavior of the `/` and `%` operators, and `bcsqrt()` throws ValueError for negative operands.
1 parent 82dc9a3 commit 84f2a98

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

ext/bcmath/bcmath.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#if HAVE_BCMATH
2424

2525
#include "php_ini.h"
26+
#include "zend_exceptions.h"
2627
#include "bcmath_arginfo.h"
2728
#include "ext/standard/info.h"
2829
#include "php_bcmath.h"
@@ -284,7 +285,7 @@ PHP_FUNCTION(bcdiv)
284285
RETVAL_STR(bc_num2str_ex(result, scale));
285286
break;
286287
case -1: /* division by zero */
287-
php_error_docref(NULL, E_WARNING, "Division by zero");
288+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
288289
break;
289290
}
290291

@@ -326,7 +327,7 @@ PHP_FUNCTION(bcmod)
326327
RETVAL_STR(bc_num2str_ex(result, scale));
327328
break;
328329
case -1:
329-
php_error_docref(NULL, E_WARNING, "Division by zero");
330+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
330331
break;
331332
}
332333

@@ -438,7 +439,7 @@ PHP_FUNCTION(bcsqrt)
438439
if (bc_sqrt (&result, scale) != 0) {
439440
RETVAL_STR(bc_num2str_ex(result, scale));
440441
} else {
441-
php_error_docref(NULL, E_WARNING, "Square root of negative number");
442+
zend_value_error("Square root of negative number");
442443
}
443444

444445
bc_free_num(&result);

ext/bcmath/bcmath.stub.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ function bcsub(string $left_operand, string $right_operand, int $scale = UNKNOWN
66

77
function bcmul(string $left_operand, string $right_operand, int $scale = UNKNOWN) : string {}
88

9-
function bcdiv(string $dividend, string $divisor, int $scale = UNKNOWN) : ?string {}
9+
function bcdiv(string $dividend, string $divisor, int $scale = UNKNOWN) : string {}
1010

11-
function bcmod(string $dividend, string $divisor, int $scale = UNKNOWN) : ?string {}
11+
function bcmod(string $dividend, string $divisor, int $scale = UNKNOWN) : string {}
1212

1313
/** @return string|false */
1414
function bcpowmod(string $base, string $exponent, string $modulus, int $scale = UNKNOWN) {}
1515

1616
function bcpow(string $base, string $exponent, int $scale = UNKNOWN) : string {}
1717

18-
function bcsqrt(string $operand, int $scale = UNKNOWN) : ?string {}
18+
function bcsqrt(string $operand, int $scale = UNKNOWN) : string {}
1919

2020
function bccomp(string $left_operand, string $right_operand, int $scale = UNKNOWN) : int {}
2121

ext/bcmath/bcmath_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ZEND_END_ARG_INFO()
1010

1111
#define arginfo_bcmul arginfo_bcadd
1212

13-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcdiv, 0, 2, IS_STRING, 1)
13+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcdiv, 0, 2, IS_STRING, 0)
1414
ZEND_ARG_TYPE_INFO(0, dividend, IS_STRING, 0)
1515
ZEND_ARG_TYPE_INFO(0, divisor, IS_STRING, 0)
1616
ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
@@ -31,7 +31,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpow, 0, 2, IS_STRING, 0)
3131
ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
3232
ZEND_END_ARG_INFO()
3333

34-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcsqrt, 0, 1, IS_STRING, 1)
34+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcsqrt, 0, 1, IS_STRING, 0)
3535
ZEND_ARG_TYPE_INFO(0, operand, IS_STRING, 0)
3636
ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
3737
ZEND_END_ARG_INFO()

ext/bcmath/tests/bcdiv_error1.phpt

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ [email protected]
88
<?php if(!extension_loaded("bcmath")) print "skip"; ?>
99
--FILE--
1010
<?php
11-
echo bcdiv('10.99', '0');
11+
try {
12+
bcdiv('10.99', '0');
13+
} catch (DivisionByZeroError $ex) {
14+
echo $ex->getMessage(), PHP_EOL;
15+
}
1216
?>
13-
--EXPECTF--
14-
Warning: bcdiv(): Division by zero in %s.php on line %d
17+
--EXPECT--
18+
Division by zero

ext/bcmath/tests/bcmod_error2.phpt

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ bcmod() - mod by 0
66
bcmath.scale=0
77
--FILE--
88
<?php
9-
echo bcmod("10", "0");
9+
try {
10+
bcmod("10", "0");
11+
} catch (DivisionByZeroError $ex) {
12+
echo $ex->getMessage(), PHP_EOL;
13+
}
1014
?>
11-
--EXPECTF--
12-
Warning: bcmod(): Division by zero in %s on line %d
15+
--EXPECT--
16+
Modulo by zero

ext/bcmath/tests/bcsqrt_error1.phpt

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ [email protected]
77
<?php if(!extension_loaded("bcmath")) print "skip"; ?>
88
--FILE--
99
<?php
10-
echo bcsqrt('-9');
10+
try {
11+
bcsqrt('-9');
12+
} catch (ValueError $ex) {
13+
echo $ex->getMessage(), PHP_EOL;
14+
}
1115
?>
12-
--EXPECTF--
13-
Warning: bcsqrt(): Square root of negative number in %s.php on line %d
16+
--EXPECT--
17+
Square root of negative number

0 commit comments

Comments
 (0)