Skip to content

Commit

Permalink
Convert UNKNOWN default values to null in ext/bcmath
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed May 2, 2020
1 parent d63eca2 commit 8a41c9e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 88 deletions.
159 changes: 88 additions & 71 deletions ext/bcmath/bcmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -273,22 +279,24 @@ 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);

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;
}

Expand Down Expand Up @@ -319,22 +327,24 @@ 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);

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;
}

Expand All @@ -356,7 +366,6 @@ PHP_FUNCTION(bcmod)
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
return;
}
/* }}} */

Expand All @@ -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);

Expand All @@ -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;
}

Expand All @@ -403,7 +414,6 @@ PHP_FUNCTION(bcpowmod)
bc_free_num(&second);
bc_free_num(&mod);
bc_free_num(&result);
return;
}
/* }}} */

Expand All @@ -412,22 +422,24 @@ 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);

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;
}

Expand All @@ -442,7 +454,6 @@ PHP_FUNCTION(bcpow)
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
return;
}
/* }}} */

Expand All @@ -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;
}

Expand All @@ -488,22 +501,24 @@ 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);

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;
}

Expand All @@ -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;
}

Expand Down
20 changes: 10 additions & 10 deletions ext/bcmath/bcmath.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Loading

0 comments on commit 8a41c9e

Please sign in to comment.