summaryrefslogtreecommitdiff
path: root/ext/bcmath
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bcmath')
-rw-r--r--ext/bcmath/bcmath.c28
-rw-r--r--ext/bcmath/php_bcmath.h2
-rw-r--r--ext/bcmath/tests/bug72093-win32.phpt18
-rw-r--r--ext/bcmath/tests/bug72093.phpt18
4 files changed, 62 insertions, 4 deletions
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index f50c555843..10450f1968 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -128,7 +128,7 @@ zend_module_entry bcmath_module_entry = {
#ifdef COMPILE_DL_BCMATH
#ifdef ZTS
-ZEND_TSRMLS_CACHE_DEFINE();
+ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(bcmath)
#endif
@@ -207,6 +207,21 @@ static void php_str2num(bc_num *num, char *str)
}
/* }}} */
+/* {{{ split_bc_num
+ Convert to bc_num detecting scale */
+static bc_num split_bc_num(bc_num num) {
+ bc_num newnum;
+ if (num->n_refs >= 1) {
+ return num;
+ }
+ newnum = _bc_new_num_ex(0, 0, 0);
+ *newnum = *num;
+ newnum->n_refs = 1;
+ num->n_refs--;
+ return newnum;
+}
+/* }}} */
+
/* {{{ proto string bcadd(string left_operand, string right_operand [, int scale])
Returns the sum of two arbitrary precision numbers */
PHP_FUNCTION(bcadd)
@@ -233,6 +248,7 @@ PHP_FUNCTION(bcadd)
bc_add (first, second, &result, scale);
if (result->n_scale > scale) {
+ result = split_bc_num(result);
result->n_scale = scale;
}
@@ -270,6 +286,7 @@ PHP_FUNCTION(bcsub)
bc_sub (first, second, &result, scale);
if (result->n_scale > scale) {
+ result = split_bc_num(result);
result->n_scale = scale;
}
@@ -307,6 +324,7 @@ PHP_FUNCTION(bcmul)
bc_multiply (first, second, &result, scale);
if (result->n_scale > scale) {
+ result = split_bc_num(result);
result->n_scale = scale;
}
@@ -345,6 +363,7 @@ PHP_FUNCTION(bcdiv)
switch (bc_divide(first, second, &result, scale)) {
case 0: /* OK */
if (result->n_scale > scale) {
+ result = split_bc_num(result);
result->n_scale = scale;
}
RETVAL_STR(bc_num2str(result));
@@ -420,8 +439,9 @@ PHP_FUNCTION(bcpowmod)
scale_int = (int) ((int)scale < 0 ? 0 : scale);
if (bc_raisemod(first, second, mod, &result, scale_int) != -1) {
- if (result->n_scale > scale) {
- result->n_scale = (int)scale;
+ if (result->n_scale > scale_int) {
+ result = split_bc_num(result);
+ result->n_scale = scale_int;
}
RETVAL_STR(bc_num2str(result));
} else {
@@ -462,6 +482,7 @@ PHP_FUNCTION(bcpow)
bc_raise (first, second, &result, scale);
if (result->n_scale > scale) {
+ result = split_bc_num(result);
result->n_scale = scale;
}
@@ -496,6 +517,7 @@ PHP_FUNCTION(bcsqrt)
if (bc_sqrt (&result, scale) != 0) {
if (result->n_scale > scale) {
+ result = split_bc_num(result);
result->n_scale = scale;
}
RETVAL_STR(bc_num2str(result));
diff --git a/ext/bcmath/php_bcmath.h b/ext/bcmath/php_bcmath.h
index 734eb7778a..9075d16b30 100644
--- a/ext/bcmath/php_bcmath.h
+++ b/ext/bcmath/php_bcmath.h
@@ -52,7 +52,7 @@ ZEND_BEGIN_MODULE_GLOBALS(bcmath)
ZEND_END_MODULE_GLOBALS(bcmath)
#if defined(ZTS) && defined(COMPILE_DL_BCMATH)
-ZEND_TSRMLS_CACHE_EXTERN();
+ZEND_TSRMLS_CACHE_EXTERN()
#endif
ZEND_EXTERN_MODULE_GLOBALS(bcmath)
diff --git a/ext/bcmath/tests/bug72093-win32.phpt b/ext/bcmath/tests/bug72093-win32.phpt
new file mode 100644
index 0000000000..a9b2077823
--- /dev/null
+++ b/ext/bcmath/tests/bug72093-win32.phpt
@@ -0,0 +1,18 @@
+--TEST--
+Bug 72093: bcpowmod accepts negative scale and corrupts _one_ definition
+--SKIPIF--
+<?php
+if(!extension_loaded("bcmath")) print "skip";
+if (substr(PHP_OS, 0, 3) != 'WIN') {
+ die('skip valid only for windows');
+}
+?>
+--FILE--
+<?php
+var_dump(bcpowmod(1, "A", 128, -200));
+var_dump(bcpowmod(1, 1.2, 1, 1));
+?>
+--EXPECTF--
+string(1) "1"
+string(3) "0.0"
+bc math warning: non-zero scale in exponent
diff --git a/ext/bcmath/tests/bug72093.phpt b/ext/bcmath/tests/bug72093.phpt
new file mode 100644
index 0000000000..911af5698f
--- /dev/null
+++ b/ext/bcmath/tests/bug72093.phpt
@@ -0,0 +1,18 @@
+--TEST--
+Bug 72093: bcpowmod accepts negative scale and corrupts _one_ definition
+--SKIPIF--
+<?php
+if(!extension_loaded("bcmath")) print "skip";
+if (substr(PHP_OS, 0, 3) == 'WIN') {
+ die('skip Not valid for windows');
+}
+?>
+--FILE--
+<?php
+var_dump(bcpowmod(1, "A", 128, -200));
+var_dump(bcpowmod(1, 1.2, 1, 1));
+?>
+--EXPECTF--
+string(1) "1"
+bc math warning: non-zero scale in exponent
+string(3) "0.0"