summaryrefslogtreecommitdiff
path: root/ext/bcmath
diff options
context:
space:
mode:
authorMáté Kocsis <kocsismate@woohoolabs.com>2020-05-01 20:49:47 +0200
committerMáté Kocsis <kocsismate@woohoolabs.com>2020-05-02 11:05:19 +0200
commit8a41c9e025b003ff26b9bf76728f3cd054474e08 (patch)
treebfcccac9c9ba16499c605df831894e004abcdd8e /ext/bcmath
parentd63eca285aca32db26e54214c0a1371a14ea9e31 (diff)
downloadphp-git-8a41c9e025b003ff26b9bf76728f3cd054474e08.tar.gz
Convert UNKNOWN default values to null in ext/bcmath
Diffstat (limited to 'ext/bcmath')
-rw-r--r--ext/bcmath/bcmath.c159
-rw-r--r--ext/bcmath/bcmath.stub.php20
-rw-r--r--ext/bcmath/bcmath_arginfo.h14
3 files changed, 105 insertions, 88 deletions
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index 61ee95b211..b7afaf04d3 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -156,22 +156,24 @@ static void php_str2num(bc_num *num, char *str)
PHP_FUNCTION(bcadd)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, result;
- int scale = BCG(bc_precision);
+ int scale;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -195,22 +197,24 @@ PHP_FUNCTION(bcadd)
PHP_FUNCTION(bcsub)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, result;
- int scale = BCG(bc_precision);
+ int scale;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -234,22 +238,24 @@ PHP_FUNCTION(bcsub)
PHP_FUNCTION(bcmul)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, result;
- int scale = BCG(bc_precision);
+ int scale;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -273,7 +279,8 @@ PHP_FUNCTION(bcmul)
PHP_FUNCTION(bcdiv)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, result;
int scale = BCG(bc_precision);
@@ -281,14 +288,15 @@ PHP_FUNCTION(bcdiv)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -319,7 +327,8 @@ PHP_FUNCTION(bcdiv)
PHP_FUNCTION(bcmod)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, result;
int scale = BCG(bc_precision);
@@ -327,14 +336,15 @@ PHP_FUNCTION(bcmod)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -356,7 +366,6 @@ PHP_FUNCTION(bcmod)
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -365,7 +374,8 @@ PHP_FUNCTION(bcmod)
PHP_FUNCTION(bcpowmod)
{
zend_string *left, *right, *modulus;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, mod, result;
int scale = BCG(bc_precision);
@@ -374,14 +384,15 @@ PHP_FUNCTION(bcpowmod)
Z_PARAM_STR(right)
Z_PARAM_STR(modulus)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 4) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(4, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(4, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -403,7 +414,6 @@ PHP_FUNCTION(bcpowmod)
bc_free_num(&second);
bc_free_num(&mod);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -412,7 +422,8 @@ PHP_FUNCTION(bcpowmod)
PHP_FUNCTION(bcpow)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second, result;
int scale = BCG(bc_precision);
@@ -420,14 +431,15 @@ PHP_FUNCTION(bcpow)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -442,7 +454,6 @@ PHP_FUNCTION(bcpow)
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
- return;
}
/* }}} */
@@ -451,21 +462,23 @@ PHP_FUNCTION(bcpow)
PHP_FUNCTION(bcsqrt)
{
zend_string *left;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num result;
int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(left)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 2) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(2, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(2, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -488,7 +501,8 @@ PHP_FUNCTION(bcsqrt)
PHP_FUNCTION(bccomp)
{
zend_string *left, *right;
- zend_long scale_param = 0;
+ zend_long scale_param;
+ zend_bool scale_param_is_null = 1;
bc_num first, second;
int scale = BCG(bc_precision);
@@ -496,14 +510,15 @@ PHP_FUNCTION(bccomp)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale_param)
+ Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
ZEND_PARSE_PARAMETERS_END();
- if (ZEND_NUM_ARGS() == 3) {
- if (scale_param < 0 || scale_param > INT_MAX) {
- zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
- RETURN_THROWS();
- }
+ if (scale_param_is_null) {
+ scale = BCG(bc_precision);
+ } else if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ } else {
scale = (int) scale_param;
}
@@ -529,19 +544,21 @@ PHP_FUNCTION(bccomp)
PHP_FUNCTION(bcscale)
{
zend_long old_scale, new_scale;
+ zend_bool new_scale_is_null = 1;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(new_scale)
+ Z_PARAM_LONG_OR_NULL(new_scale, new_scale_is_null)
ZEND_PARSE_PARAMETERS_END();
old_scale = BCG(bc_precision);
- if (ZEND_NUM_ARGS() == 1) {
+ if (!new_scale_is_null) {
if (new_scale < 0 || new_scale > INT_MAX) {
zend_argument_value_error(1, "must be between 0 and %d", INT_MAX);
RETURN_THROWS();
}
+
BCG(bc_precision) = (int) new_scale;
}
diff --git a/ext/bcmath/bcmath.stub.php b/ext/bcmath/bcmath.stub.php
index 6ef6cc0926..45e75376f0 100644
--- a/ext/bcmath/bcmath.stub.php
+++ b/ext/bcmath/bcmath.stub.php
@@ -2,22 +2,22 @@
/** @generate-function-entries */
-function bcadd(string $left_operand, string $right_operand, int $scale = UNKNOWN): string {}
+function bcadd(string $left_operand, string $right_operand, ?int $scale = null): string {}
-function bcsub(string $left_operand, string $right_operand, int $scale = UNKNOWN): string {}
+function bcsub(string $left_operand, string $right_operand, ?int $scale = null): string {}
-function bcmul(string $left_operand, string $right_operand, int $scale = UNKNOWN): string {}
+function bcmul(string $left_operand, string $right_operand, ?int $scale = null): string {}
-function bcdiv(string $dividend, string $divisor, int $scale = UNKNOWN): string {}
+function bcdiv(string $dividend, string $divisor, ?int $scale = null): string {}
-function bcmod(string $dividend, string $divisor, int $scale = UNKNOWN): string {}
+function bcmod(string $dividend, string $divisor, ?int $scale = null): string {}
-function bcpowmod(string $base, string $exponent, string $modulus, int $scale = UNKNOWN): string|false {}
+function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string|false {}
-function bcpow(string $base, string $exponent, int $scale = UNKNOWN): string {}
+function bcpow(string $base, string $exponent, ?int $scale = null): string {}
-function bcsqrt(string $operand, int $scale = UNKNOWN): string {}
+function bcsqrt(string $operand, ?int $scale = null): string {}
-function bccomp(string $left_operand, string $right_operand, int $scale = UNKNOWN): int {}
+function bccomp(string $left_operand, string $right_operand, ?int $scale = null): int {}
-function bcscale(int $scale = UNKNOWN): int {}
+function bcscale(?int $scale = null): int {}
diff --git a/ext/bcmath/bcmath_arginfo.h b/ext/bcmath/bcmath_arginfo.h
index 61f0092b45..75f864dc9d 100644
--- a/ext/bcmath/bcmath_arginfo.h
+++ b/ext/bcmath/bcmath_arginfo.h
@@ -3,7 +3,7 @@
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcadd, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, left_operand, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, right_operand, IS_STRING, 0)
- ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
+ ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
#define arginfo_bcsub arginfo_bcadd
@@ -13,7 +13,7 @@ ZEND_END_ARG_INFO()
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)
+ ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
#define arginfo_bcmod arginfo_bcdiv
@@ -22,28 +22,28 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bcpowmod, 0, 3, MAY_BE_STRING|MA
ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, modulus, IS_STRING, 0)
- ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
+ ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpow, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0)
- ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
+ ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
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_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bccomp, 0, 2, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, left_operand, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, right_operand, IS_STRING, 0)
- ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
+ ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcscale, 0, 0, IS_LONG, 0)
- ZEND_ARG_TYPE_INFO(0, scale, IS_LONG, 0)
+ ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, scale, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()