From f573b6774bd79fc3690089c65d6635026b4b8e68 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 28 Oct 2019 10:08:57 +0100 Subject: [PATCH 1/2] Elevate warnings to ValueError in ext/bcmath All of these warnings/ValueErrors are due to programming errors, i.e. calling a function with unsupported arguments. In particular, this matches the behavior of the `/` and `%` operators. --- ext/bcmath/bcmath.c | 6 +++--- ext/bcmath/bcmath.stub.php | 6 +++--- ext/bcmath/bcmath_arginfo.h | 4 ++-- ext/bcmath/tests/bcdiv_error1.phpt | 10 +++++++--- ext/bcmath/tests/bcmod_error2.phpt | 10 +++++++--- ext/bcmath/tests/bcsqrt_error1.phpt | 10 +++++++--- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index f528922752bf..2c38a24bd4a3 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -284,7 +284,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_value_error("Division by zero"); break; } @@ -326,7 +326,7 @@ PHP_FUNCTION(bcmod) RETVAL_STR(bc_num2str_ex(result, scale)); break; case -1: - php_error_docref(NULL, E_WARNING, "Division by zero"); + zend_value_error("Division by zero"); break; } @@ -438,7 +438,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); diff --git a/ext/bcmath/bcmath.stub.php b/ext/bcmath/bcmath.stub.php index b5d1816accd1..274759b8a6d2 100644 --- a/ext/bcmath/bcmath.stub.php +++ b/ext/bcmath/bcmath.stub.php @@ -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 {} diff --git a/ext/bcmath/bcmath_arginfo.h b/ext/bcmath/bcmath_arginfo.h index 6c0b1061f2f7..2d2117acaa17 100644 --- a/ext/bcmath/bcmath_arginfo.h +++ b/ext/bcmath/bcmath_arginfo.h @@ -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() diff --git a/ext/bcmath/tests/bcdiv_error1.phpt b/ext/bcmath/tests/bcdiv_error1.phpt index c69d36bb9ce4..b504212735eb 100644 --- a/ext/bcmath/tests/bcdiv_error1.phpt +++ b/ext/bcmath/tests/bcdiv_error1.phpt @@ -8,7 +8,11 @@ antoni@solucionsinternet.com --FILE-- getMessage(), PHP_EOL; +} ?> ---EXPECTF-- -Warning: bcdiv(): Division by zero in %s.php on line %d +--EXPECT-- +Division by zero diff --git a/ext/bcmath/tests/bcmod_error2.phpt b/ext/bcmath/tests/bcmod_error2.phpt index 714615d7d158..eba610f7ff27 100644 --- a/ext/bcmath/tests/bcmod_error2.phpt +++ b/ext/bcmath/tests/bcmod_error2.phpt @@ -6,7 +6,11 @@ bcmod() - mod by 0 bcmath.scale=0 --FILE-- getMessage(), PHP_EOL; +} ?> ---EXPECTF-- -Warning: bcmod(): Division by zero in %s on line %d +--EXPECT-- +Division by zero diff --git a/ext/bcmath/tests/bcsqrt_error1.phpt b/ext/bcmath/tests/bcsqrt_error1.phpt index 1f213dbc8212..bbc69f3952b0 100644 --- a/ext/bcmath/tests/bcsqrt_error1.phpt +++ b/ext/bcmath/tests/bcsqrt_error1.phpt @@ -7,7 +7,11 @@ antoni@solucionsinternet.com --FILE-- getMessage(), PHP_EOL; +} ?> ---EXPECTF-- -Warning: bcsqrt(): Square root of negative number in %s.php on line %d +--EXPECT-- +Square root of negative number From 1e3299925c671b4a55fd358afdba863a8aff37e0 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 28 Oct 2019 11:59:20 +0100 Subject: [PATCH 2/2] Change ValueError to DivisionByZeroError --- ext/bcmath/bcmath.c | 5 +++-- ext/bcmath/tests/bcdiv_error1.phpt | 2 +- ext/bcmath/tests/bcmod_error2.phpt | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 2c38a24bd4a3..5d0d47f7c002 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -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 */ - zend_value_error("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: - zend_value_error("Division by zero"); + zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero"); break; } diff --git a/ext/bcmath/tests/bcdiv_error1.phpt b/ext/bcmath/tests/bcdiv_error1.phpt index b504212735eb..a89ae98cfbba 100644 --- a/ext/bcmath/tests/bcdiv_error1.phpt +++ b/ext/bcmath/tests/bcdiv_error1.phpt @@ -10,7 +10,7 @@ antoni@solucionsinternet.com getMessage(), PHP_EOL; } ?> diff --git a/ext/bcmath/tests/bcmod_error2.phpt b/ext/bcmath/tests/bcmod_error2.phpt index eba610f7ff27..c4d49486b3c5 100644 --- a/ext/bcmath/tests/bcmod_error2.phpt +++ b/ext/bcmath/tests/bcmod_error2.phpt @@ -8,9 +8,9 @@ bcmath.scale=0 getMessage(), PHP_EOL; } ?> --EXPECT-- -Division by zero +Modulo by zero