]> granicus.if.org Git - php/commitdiff
Convert UNKNOWN default values to null in ext/bcmath
authorMáté Kocsis <kocsismate@woohoolabs.com>
Fri, 1 May 2020 18:49:47 +0000 (20:49 +0200)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Sat, 2 May 2020 09:05:19 +0000 (11:05 +0200)
ext/bcmath/bcmath.c
ext/bcmath/bcmath.stub.php
ext/bcmath/bcmath_arginfo.h

index 61ee95b2118a2f32e190a44237640fc5d3e651a4..b7afaf04d32ec40a981988c1d99a6980ff660e37 100644 (file)
@@ -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;
        }
 
index 6ef6cc0926f4a3e1ce7ac3b9d09aafd89ab5e129..45e75376f0991105b2dbd357b2d4434146a472c0 100644 (file)
@@ -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 {}
index 61f0092b4524ca5d2081836a6f1ec643e14d168b..75f864dc9d36f2cecc957db2b83632b32467d743 100644 (file)
@@ -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()