]> granicus.if.org Git - php/commitdiff
Elevate warnings to Error Exceptions in ext/bcmath
authorChristoph M. Becker <cmbecker69@gmx.de>
Mon, 28 Oct 2019 10:59:20 +0000 (11:59 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Mon, 28 Oct 2019 11:22:31 +0000 (12:22 +0100)
`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.

ext/bcmath/bcmath.c
ext/bcmath/bcmath.stub.php
ext/bcmath/bcmath_arginfo.h
ext/bcmath/tests/bcdiv_error1.phpt
ext/bcmath/tests/bcmod_error2.phpt
ext/bcmath/tests/bcsqrt_error1.phpt

index f528922752bfc93cbd23325c688d7188d838f337..5d0d47f7c00269f701d37cb87bcf2e379c1cd7fe 100644 (file)
@@ -23,6 +23,7 @@
 #if HAVE_BCMATH
 
 #include "php_ini.h"
+#include "zend_exceptions.h"
 #include "bcmath_arginfo.h"
 #include "ext/standard/info.h"
 #include "php_bcmath.h"
@@ -284,7 +285,7 @@ PHP_FUNCTION(bcdiv)
                        RETVAL_STR(bc_num2str_ex(result, scale));
                        break;
                case -1: /* division by zero */
-                       php_error_docref(NULL, E_WARNING, "Division by zero");
+                       zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
                        break;
        }
 
@@ -326,7 +327,7 @@ PHP_FUNCTION(bcmod)
                        RETVAL_STR(bc_num2str_ex(result, scale));
                        break;
                case -1:
-                       php_error_docref(NULL, E_WARNING, "Division by zero");
+                       zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
                        break;
        }
 
@@ -438,7 +439,7 @@ PHP_FUNCTION(bcsqrt)
        if (bc_sqrt (&result, scale) != 0) {
                RETVAL_STR(bc_num2str_ex(result, scale));
        } else {
-               php_error_docref(NULL, E_WARNING, "Square root of negative number");
+               zend_value_error("Square root of negative number");
        }
 
        bc_free_num(&result);
index b5d1816accd17043538068a9621d32e5f1d2661f..274759b8a6d25a6f8563ad605d1ac572ad36fae4 100644 (file)
@@ -6,16 +6,16 @@ function bcsub(string $left_operand, string $right_operand, int $scale = UNKNOWN
 
 function bcmul(string $left_operand, string $right_operand, int $scale = UNKNOWN) : string {}
 
-function bcdiv(string $dividend, string $divisor, int $scale = UNKNOWN) : ?string {}
+function bcdiv(string $dividend, string $divisor, int $scale = UNKNOWN) : string {}
 
-function bcmod(string $dividend, string $divisor, int $scale = UNKNOWN) : ?string {}
+function bcmod(string $dividend, string $divisor, int $scale = UNKNOWN) : string {}
 
 /** @return string|false */
 function bcpowmod(string $base, string $exponent, string $modulus, int $scale = UNKNOWN) {}
 
 function bcpow(string $base, string $exponent, int $scale = UNKNOWN) : string {}
 
-function bcsqrt(string $operand, int $scale = UNKNOWN) : ?string {}
+function bcsqrt(string $operand, int $scale = UNKNOWN) : string {}
 
 function bccomp(string $left_operand, string $right_operand, int $scale = UNKNOWN) : int {}
 
index 6c0b1061f2f787821e56850167e4849847efd3cf..2d2117acaa1781045ec3784031cd51cc716232af 100644 (file)
@@ -10,7 +10,7 @@ ZEND_END_ARG_INFO()
 
 #define arginfo_bcmul arginfo_bcadd
 
-ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcdiv, 0, 2, IS_STRING, 1)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcdiv, 0, 2, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, dividend, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, divisor, IS_STRING, 0)
        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)
        ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcsqrt, 0, 1, IS_STRING, 1)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcsqrt, 0, 1, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, operand, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
 ZEND_END_ARG_INFO()
index c69d36bb9ce4e10d27c2d8655c26b000ed8ad1d2..a89ae98cfbbafedf9865ba34c0c0d21ad824cc65 100644 (file)
@@ -8,7 +8,11 @@ antoni@solucionsinternet.com
 <?php if(!extension_loaded("bcmath")) print "skip"; ?>
 --FILE--
 <?php
-echo bcdiv('10.99', '0');
+try {
+    bcdiv('10.99', '0');
+} catch (DivisionByZeroError $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
 ?>
---EXPECTF--
-Warning: bcdiv(): Division by zero in %s.php on line %d
+--EXPECT--
+Division by zero
index 714615d7d15897669e3655fd81ee2eaf259a33cf..c4d49486b3c5ab864617625f0eb7614ab9bd1533 100644 (file)
@@ -6,7 +6,11 @@ bcmod() - mod by 0
 bcmath.scale=0
 --FILE--
 <?php
-echo bcmod("10", "0");
+try {
+    bcmod("10", "0");
+} catch (DivisionByZeroError $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
 ?>
---EXPECTF--
-Warning: bcmod(): Division by zero in %s on line %d
+--EXPECT--
+Modulo by zero
index 1f213dbc821208439456e3e279450402f01dda6c..bbc69f3952b007f0e9c8f51862dce568e57e62c3 100644 (file)
@@ -7,7 +7,11 @@ antoni@solucionsinternet.com
 <?php if(!extension_loaded("bcmath")) print "skip"; ?>
 --FILE--
 <?php
-echo bcsqrt('-9');
+try {
+    bcsqrt('-9');
+} catch (ValueError $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
 ?>
---EXPECTF--
-Warning: bcsqrt(): Square root of negative number in %s.php on line %d
+--EXPECT--
+Square root of negative number