PHP_BCMATH_VERSION,
PHP_MODULE_GLOBALS(bcmath),
PHP_GINIT(bcmath),
- PHP_GSHUTDOWN(bcmath),
+ PHP_GSHUTDOWN(bcmath),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
ZEND_GET_MODULE(bcmath)
#endif
+ZEND_INI_MH(OnUpdateScale)
+{
+ int *p;
+ zend_long tmp;
+
+ tmp = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
+ if (tmp < 0 || tmp > INT_MAX) {
+ return FAILURE;
+ }
+
+ p = (int *) ZEND_INI_GET_ADDR();
+ *p = (int) tmp;
+
+ return SUCCESS;
+}
+
/* {{{ PHP_INI */
PHP_INI_BEGIN()
- STD_PHP_INI_ENTRY("bcmath.scale", "0", PHP_INI_ALL, OnUpdateLongGEZero, bc_precision, zend_bcmath_globals, bcmath_globals)
+ STD_PHP_INI_ENTRY("bcmath.scale", "0", PHP_INI_ALL, OnUpdateScale, bc_precision, zend_bcmath_globals, bcmath_globals)
PHP_INI_END()
/* }}} */
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second, result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) (scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second, result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second, result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second, result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second, result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
PHP_FUNCTION(bcpowmod)
{
zend_string *left, *right, *modulus;
+ zend_long scale_param = 0;
bc_num first, second, mod, result;
- zend_long scale = BCG(bc_precision);
- int scale_int;
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_STR(left)
Z_PARAM_STR(right)
Z_PARAM_STR(modulus)
Z_PARAM_OPTIONAL
- Z_PARAM_LONG(scale)
+ Z_PARAM_LONG(scale_param)
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();
+ }
+ scale = (int) scale_param;
+ }
+
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&mod);
php_str2num(&second, ZSTR_VAL(right));
php_str2num(&mod, ZSTR_VAL(modulus));
- scale_int = (int) ((int)scale < 0 ? 0 : scale);
-
- if (bc_raisemod(first, second, mod, &result, scale_int) != -1) {
- RETVAL_STR(bc_num2str_ex(result, scale_int));
+ if (bc_raisemod(first, second, mod, &result, scale) != -1) {
+ RETVAL_STR(bc_num2str_ex(result, scale));
} else {
RETVAL_FALSE;
}
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second, result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
zend_string *left;
zend_long scale_param = 0;
bc_num result;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 2) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(2, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&result);
zend_string *left, *right;
zend_long scale_param = 0;
bc_num first, second;
- int scale = (int)BCG(bc_precision);
+ int scale = BCG(bc_precision);
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(left)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_NUM_ARGS() == 3) {
- scale = (int) ((int)scale_param < 0 ? 0 : scale_param);
+ if (scale_param < 0 || scale_param > INT_MAX) {
+ zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
+ RETURN_THROWS();
+ }
+ scale = (int) scale_param;
}
bc_init_num(&first);
php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
}
if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
}
RETVAL_LONG(bc_compare(first, second));
old_scale = BCG(bc_precision);
if (ZEND_NUM_ARGS() == 1) {
- BCG(bc_precision) = ((int)new_scale < 0) ? 0 : new_scale;
+ 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;
}
RETURN_LONG(old_scale);
--- /dev/null
+--TEST--
+all errors on negative scale
+--SKIPIF--
+<?php if(!extension_loaded("bcmath")) print "skip"; ?>
+--INI--
+bcmath.scale=0
+--FILE--
+<?php
+try {
+ bcadd('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcsub('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcmul('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcdiv('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcmod('1','2',-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcpowmod('1', '2', '3', -9);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcpow('1', '2', -1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcsqrt('9', -1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bccomp('1', '2', -1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+try {
+ bcscale(-1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . \PHP_EOL;
+}
+?>
+--EXPECT--
+bcadd(): Argument #3 ($scale) must be between 0 and 2147483647
+bcsub(): Argument #3 ($scale) must be between 0 and 2147483647
+bcmul(): Argument #3 ($scale) must be between 0 and 2147483647
+bcdiv(): Argument #3 ($scale) must be between 0 and 2147483647
+bcmod(): Argument #3 ($scale) must be between 0 and 2147483647
+bcpowmod(): Argument #4 ($scale) must be between 0 and 2147483647
+bcpow(): Argument #3 ($scale) must be between 0 and 2147483647
+bcsqrt(): Argument #2 ($scale) must be between 0 and 2147483647
+bccomp(): Argument #3 ($scale) must be between 0 and 2147483647
+bcscale(): Argument #1 ($scale) must be between 0 and 2147483647