Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions ext/bcmath/bcmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -188,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;
Expand Down Expand Up @@ -238,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;
Expand Down Expand Up @@ -288,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;
Expand Down Expand Up @@ -338,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;
Expand Down Expand Up @@ -393,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;
Expand Down Expand Up @@ -449,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;
Expand Down Expand Up @@ -526,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;
Expand Down Expand Up @@ -588,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;
Expand Down Expand Up @@ -633,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;
Expand Down Expand Up @@ -774,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();
}

Expand Down Expand Up @@ -1305,15 +1304,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, bool scale_is_null, uint32_t arg_num)
{
if (UNEXPECTED(!scale_is_null && (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;
Expand Down Expand Up @@ -1359,7 +1349,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;
}

Expand Down Expand Up @@ -1469,7 +1459,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;
}

Expand Down Expand Up @@ -1530,7 +1520,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();
}

Expand Down Expand Up @@ -1584,7 +1574,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;
}

Expand Down
Loading