com php-src: Fixed bug #66364 (BCMath bcmul ignores scale parameter): ext/bcmath/bcmath.c ext/bcmath/libbcmath/src/bcmath.h
ext/bcmath/libbcmath/src/num2str.c ext/bcmath/tests/bcpow.phpt ext/bcmath/tests/bug.66364.phpt ext/bcmath/tests/scale.phpt
ext/bcmath/tests/scale_ini.phpt
| From: | Christoph Michael Becker | Date: | Fri, 08 Sep 2017 16:31:04 +0000 |
| Subject: | com php-src: Fixed bug #66364 (BCMath bcmul ignores scale parameter): ext/bcmath/bcmath.c ext/bcmath/libbcmath/src/bcmath.h ext/bcmath/libbcmath/src/num2str.c ext/bcmath/tests/bcpow.phpt ext/bcmath/tests/bug.66364.phpt ext/bcmath/tests/scale.phpt ext/bcmath/tests/scale_ini.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-99454@lists.php.net to get a copy of this message | ||
Commit: 870ed5106d6274905b5aa4341429deef12c92e55
Author: Christoph M. Becker <cmbecker69@gmx.de> Fri, 8 Sep 2017 18:31:04 +0200
Parents: 657695fe9777d0e91ee988afc8cb8a07120bdd2d
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=870ed5106d6274905b5aa4341429deef12c92e55
Log:
Fixed bug #66364 (BCMath bcmul ignores scale parameter)
We change
bcmul() and bcpow() so that the result has exactly the
requested scale (i.e. decimal places) to make them consistent with the
other BCMath functions. This also changes our stance regarding bug
#52748, which had been classified as documentation problem.
We do not manipulate the numbers themselves (anymore), but rather
introduce bc_num2str_ex() which accepts a scale parameter that
overrides the scale of the number by omitting extraneous decimals and
adding zeros, respectively. This also allows us to get rid of
split_bc_num(), which fixes bug #75164 as well.
Bugs:
https://bugs.php.net/66364
https://bugs.php.net/52748
https://bugs.php.net/75164
Changed paths:
M ext/bcmath/bcmath.c
M ext/bcmath/libbcmath/src/bcmath.h
M ext/bcmath/libbcmath/src/num2str.c
M ext/bcmath/tests/bcpow.phpt
A ext/bcmath/tests/bug.66364.phpt
A ext/bcmath/tests/scale.phpt
A ext/bcmath/tests/scale_ini.phpt
Diff:
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c
index e8b87dc..dc51f92 100644
--- a/ext/bcmath/bcmath.c
+++ b/ext/bcmath/bcmath.c
@@ -207,21 +207,6 @@ 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)
@@ -249,12 +234,7 @@ PHP_FUNCTION(bcadd)
php_str2num(&second, ZSTR_VAL(right));
bc_add (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -289,12 +269,7 @@ PHP_FUNCTION(bcsub)
php_str2num(&second, ZSTR_VAL(right));
bc_sub (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -329,12 +304,7 @@ PHP_FUNCTION(bcmul)
php_str2num(&second, ZSTR_VAL(right));
bc_multiply (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -370,11 +340,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));
+ RETVAL_STR(bc_num2str_ex(result, scale));
break;
case -1: /* division by zero */
php_error_docref(NULL, E_WARNING, "Division by zero");
@@ -450,11 +416,7 @@ 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_int) {
- result = split_bc_num(result);
- result->n_scale = scale_int;
- }
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale_int));
} else {
RETVAL_FALSE;
}
@@ -494,12 +456,7 @@ PHP_FUNCTION(bcpow)
php_str2num(&second, ZSTR_VAL(right));
bc_raise (first, second, &result, scale);
- if (result->n_scale > scale) {
- result = split_bc_num(result);
- result->n_scale = scale;
- }
-
- RETVAL_STR(bc_num2str(result));
+ RETVAL_STR(bc_num2str_ex(result, scale));
bc_free_num(&first);
bc_free_num(&second);
bc_free_num(&result);
@@ -530,11 +487,7 @@ PHP_FUNCTION(bcsqrt)
php_str2num(&result, ZSTR_VAL(left));
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));
+ RETVAL_STR(bc_num2str_ex(result, scale));
} else {
php_error_docref(NULL, E_WARNING, "Square root of negative number");
}
diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h
index b83ba13..08c5480 100644
--- a/ext/bcmath/libbcmath/src/bcmath.h
+++ b/ext/bcmath/libbcmath/src/bcmath.h
@@ -110,7 +110,7 @@ _PROTOTYPE(void bc_init_num, (bc_num *num));
_PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale));
-_PROTOTYPE(zend_string *bc_num2str, (bc_num num));
+_PROTOTYPE(zend_string *bc_num2str_ex, (bc_num num, int scale));
_PROTOTYPE(void bc_int2num, (bc_num *num, int val));
@@ -157,5 +157,6 @@ _PROTOTYPE(void bc_out_of_memory, (void));
#define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0)
#define bc_free_num(num) _bc_free_num_ex((num), 0)
+#define bc_num2str(num) bc_num2str_ex((num), (num->n_scale))
#endif
diff --git a/ext/bcmath/libbcmath/src/num2str.c b/ext/bcmath/libbcmath/src/num2str.c
index c72a924..7316d32 100644
--- a/ext/bcmath/libbcmath/src/num2str.c
+++ b/ext/bcmath/libbcmath/src/num2str.c
@@ -41,8 +41,9 @@
/* Convert a numbers to a string. Base 10 only.*/
zend_string
-*bc_num2str (num)
+*bc_num2str_ex (num, scale)
bc_num num;
+ int scale;
{
zend_string *str;
char *sptr;
@@ -51,8 +52,8 @@ zend_string
/* Allocate the string memory. */
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
- if (num->n_scale > 0)
- str = zend_string_alloc(num->n_len + num->n_scale + signch + 1, 0);
+ if (scale > 0)
+ str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
else
str = zend_string_alloc(num->n_len + signch, 0);
if (str == NULL) bc_out_of_memory();
@@ -67,11 +68,13 @@ zend_string
*sptr++ = BCD_CHAR(*nptr++);
/* Now the fraction. */
- if (num->n_scale > 0)
+ if (scale > 0)
{
*sptr++ = '.';
- for (index=0; index<num->n_scale; index++)
+ for (index=0; index<scale && index<num->n_scale; index++)
*sptr++ = BCD_CHAR(*nptr++);
+ for (index = num->n_scale; index<scale; index++)
+ *sptr++ = BCD_CHAR(0);
}
/* Terminate the string and return it! */
diff --git a/ext/bcmath/tests/bcpow.phpt b/ext/bcmath/tests/bcpow.phpt
index 141ec61..ad642a5 100644
--- a/ext/bcmath/tests/bcpow.phpt
+++ b/ext/bcmath/tests/bcpow.phpt
@@ -13,6 +13,6 @@ echo bcpow("-2.555", "5", 2),"\n";
?>
--EXPECT--
1
--32
+-32.0000
18446744073709551616
-108.88
diff --git a/ext/bcmath/tests/bug.66364.phpt b/ext/bcmath/tests/bug.66364.phpt
new file mode 100644
index 0000000..564f40e
--- /dev/null
+++ b/ext/bcmath/tests/bug.66364.phpt
@@ -0,0 +1,14 @@
+--TEST--
+Bug #66364 (BCMath bcmul ignores scale parameter)
+--SKIPIF--
+<?php
+if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
+?>
+--FILE--
+<?php
+var_dump(bcmul('0.3', '0.2', 4));
+?>
+===DONE===
+--EXPECT--
+string(6) "0.0600"
+===DONE===
diff --git a/ext/bcmath/tests/scale.phpt b/ext/bcmath/tests/scale.phpt
new file mode 100644
index 0000000..d49e530
--- /dev/null
+++ b/ext/bcmath/tests/scale.phpt
@@ -0,0 +1,27 @@
+--TEST--
+BCMath functions return result with requested scale
+--SKIPIF--
+<?php
+if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
+?>
+--FILE--
+<?php
+echo
+ 'bcadd: ', bcadd('2', '1', 5), PHP_EOL,
+ 'bcdiv: ', bcdiv('2', '1', 5), PHP_EOL,
+ 'bcmul: ', bcmul('2', '1', 5), PHP_EOL,
+ 'bcpow: ', bcpow('2', '1', 5), PHP_EOL,
+ 'bcpowmod: ', bcpowmod('2', '1', '3', 5), PHP_EOL,
+ 'bcsqrt: ', bcsqrt('4', 5), PHP_EOL,
+ 'bcsub: ', bcsub('2', '1', 5), PHP_EOL;
+?>
+===DONE===
+--EXPECT--
+bcadd: 3.00000
+bcdiv: 2.00000
+bcmul: 2.00000
+bcpow: 2.00000
+bcpowmod: 2.00000
+bcsqrt: 2.00000
+bcsub: 1.00000
+===DONE===
diff --git a/ext/bcmath/tests/scale_ini.phpt b/ext/bcmath/tests/scale_ini.phpt
new file mode 100644
index 0000000..66d9d48
--- /dev/null
+++ b/ext/bcmath/tests/scale_ini.phpt
@@ -0,0 +1,29 @@
+--TEST--
+BCMath functions return result with default scale
+--SKIPIF--
+<?php
+if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
+?>
+--INI--
+bcmath.scale = 5
+--FILE--
+<?php
+echo
+ 'bcadd: ', bcadd('2', '1'), PHP_EOL,
+ 'bcdiv: ', bcdiv('2', '1'), PHP_EOL,
+ 'bcmul: ', bcmul('2', '1'), PHP_EOL,
+ 'bcpow: ', bcpow('2', '1'), PHP_EOL,
+ 'bcpowmod: ', bcpowmod('2', '1', '3'), PHP_EOL,
+ 'bcsqrt: ', bcsqrt('4'), PHP_EOL,
+ 'bcsub: ', bcsub('2', '1'), PHP_EOL;
+?>
+===DONE===
+--EXPECT--
+bcadd: 3.00000
+bcdiv: 2.00000
+bcmul: 2.00000
+bcpow: 2.00000
+bcpowmod: 2.00000
+bcsqrt: 2.00000
+bcsub: 1.00000
+===DONE===