From 9f7faaf62e2749f10e92f97a498f539cf55c9ea6 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 6 Sep 2024 21:14:01 +0200 Subject: [PATCH 1/3] Simplify bcmath_check_scale() The scale is default-initialized to 0, so we can simplify the code a bit. --- ext/bcmath/bcmath.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 08f6b7c14cb90..fcf38a1dab8c8 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -1305,9 +1305,9 @@ static zend_always_inline zend_result bc_num_from_obj_or_str_or_long_with_err( return SUCCESS; } -static zend_always_inline zend_result bcmath_check_scale(zend_long scale, bool scale_is_null, uint32_t arg_num) +static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32_t arg_num) { - if (UNEXPECTED(!scale_is_null && (scale < 0 || scale > INT_MAX))) { + if (UNEXPECTED(scale < 0 || scale > INT_MAX)) { zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX); return FAILURE; } @@ -1359,7 +1359,7 @@ static void bcmath_number_calc_method(INTERNAL_FUNCTION_PARAMETERS, uint8_t opco if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) { goto fail; } - if (bcmath_check_scale(scale_lval, scale_is_null, 2) == FAILURE) { + if (bcmath_check_scale(scale_lval, 2) == FAILURE) { goto fail; } @@ -1469,7 +1469,7 @@ PHP_METHOD(BcMath_Number, powmod) if (bc_num_from_obj_or_str_or_long_with_err(&modulus_num, NULL, modulus_obj, modulus_str, modulus_lval, 2) == FAILURE) { goto cleanup; } - if (bcmath_check_scale(scale_lval, scale_is_null, 3) == FAILURE) { + if (bcmath_check_scale(scale_lval, 3) == FAILURE) { goto cleanup; } @@ -1530,7 +1530,7 @@ PHP_METHOD(BcMath_Number, sqrt) Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null); ZEND_PARSE_PARAMETERS_END(); - if (bcmath_check_scale(scale_lval, scale_is_null, 1) == FAILURE) { + if (bcmath_check_scale(scale_lval, 1) == FAILURE) { RETURN_THROWS(); } @@ -1584,7 +1584,7 @@ PHP_METHOD(BcMath_Number, compare) if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) { goto fail; } - if (bcmath_check_scale(scale_lval, scale_is_null, 2) == FAILURE) { + if (bcmath_check_scale(scale_lval, 2) == FAILURE) { goto fail; } From 3d4cfd898387cc399a9ea935ec1a259e5b29464b Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 6 Sep 2024 21:19:18 +0200 Subject: [PATCH 2/3] Move bcmath_check_scale() --- ext/bcmath/bcmath.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index fcf38a1dab8c8..cd9fcde9dcc75 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -148,6 +148,15 @@ PHP_MINFO_FUNCTION(bcmath) } /* }}} */ +static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32_t arg_num) +{ + if (UNEXPECTED(scale < 0 || scale > INT_MAX)) { + zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX); + return FAILURE; + } + return SUCCESS; +} + static void php_long2num(bc_num *num, zend_long lval) { *num = bc_long2num(lval); @@ -1305,15 +1314,6 @@ static zend_always_inline zend_result bc_num_from_obj_or_str_or_long_with_err( return SUCCESS; } -static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32_t arg_num) -{ - if (UNEXPECTED(scale < 0 || scale > INT_MAX)) { - zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX); - return FAILURE; - } - return SUCCESS; -} - PHP_METHOD(BcMath_Number, __construct) { zend_string *str = NULL; From 42ada02d5154d5632049ed3d54d65c4144217553 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 6 Sep 2024 21:19:24 +0200 Subject: [PATCH 3/3] Reuse bcmath_check_scale() --- ext/bcmath/bcmath.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index cd9fcde9dcc75..b7fef270c93d3 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -197,8 +197,7 @@ PHP_FUNCTION(bcadd) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -247,8 +246,7 @@ PHP_FUNCTION(bcsub) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -297,8 +295,7 @@ PHP_FUNCTION(bcmul) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -347,8 +344,7 @@ PHP_FUNCTION(bcdiv) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -402,8 +398,7 @@ PHP_FUNCTION(bcmod) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -458,8 +453,7 @@ PHP_FUNCTION(bcpowmod) 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); + } else if (bcmath_check_scale(scale_param, 4) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -535,8 +529,7 @@ PHP_FUNCTION(bcpow) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -597,8 +590,7 @@ PHP_FUNCTION(bcsqrt) 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); + } else if (bcmath_check_scale(scale_param, 2) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -642,8 +634,7 @@ PHP_FUNCTION(bccomp) 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); + } else if (bcmath_check_scale(scale_param, 3) == FAILURE) { RETURN_THROWS(); } else { scale = (int) scale_param; @@ -783,8 +774,7 @@ PHP_FUNCTION(bcscale) old_scale = BCG(bc_precision); 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); + if (bcmath_check_scale(new_scale, 1) == FAILURE) { RETURN_THROWS(); }