com php-src: Fixed bug #54598 (bcpowmod() may return 1 if modulus is 1): NEWS ext/bcmath/libbcmath/src/raisemod.c ext/bcmath/tests/bug54598.phpt
| From: | Christoph Michael Becker | Date: | Wed, 06 Sep 2017 22:30:05 +0000 |
| Subject: | com php-src: Fixed bug #54598 (bcpowmod() may return 1 if modulus is 1): NEWS ext/bcmath/libbcmath/src/raisemod.c ext/bcmath/tests/bug54598.phpt | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-99293@lists.php.net to get a copy of this message | ||
Commit: b2919853f867ca7948e1c80c48eb868eae4ba882
Author: Christoph M. Becker <cmbecker69@gmx.de> Thu, 7 Sep 2017 00:30:05 +0200
Parents: dea41f3c3a003a22893b27eead429f963d3230c3
Branches: PHP-7.0 PHP-7.1 PHP-7.2 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=b2919853f867ca7948e1c80c48eb868eae4ba882
Log:
Fixed bug #54598 (bcpowmod() may return 1 if modulus is 1)
x mod 1 is always zero; we have to take the scale into account,
though.
Bugs:
https://bugs.php.net/54598
Changed paths:
M NEWS
M ext/bcmath/libbcmath/src/raisemod.c
A ext/bcmath/tests/bug54598.phpt
Diff:
diff --git a/NEWS b/NEWS
index 377f4d1..7fb0cb1 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ PHP NEWS
- BCMath:
. Fixed bug #44995 (bcpowmod() fails if scale != 0). (cmb)
+ . Fixed bug #54598 (bcpowmod() may return 1 if modulus is 1). (okano1220, cmb)
- CLI server:
. Fixed bug #70470 (Built-in server truncates headers spanning over TCP
diff --git a/ext/bcmath/libbcmath/src/raisemod.c b/ext/bcmath/libbcmath/src/raisemod.c
index 72a838a..84a7321 100644
--- a/ext/bcmath/libbcmath/src/raisemod.c
+++ b/ext/bcmath/libbcmath/src/raisemod.c
@@ -75,17 +75,24 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
/* Do the calculation. */
rscale = MAX(scale, base->n_scale);
- while ( !bc_is_zero(exponent) )
+ if ( !bc_compare(mod, BCG(_one_)) )
{
- (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
- if ( !bc_is_zero(parity) )
+ temp = bc_new_num (1, scale);
+ }
+ else
+ {
+ while ( !bc_is_zero(exponent) )
{
- bc_multiply (temp, power, &temp, rscale);
- (void) bc_modulo (temp, mod, &temp, scale);
+ (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
+ if ( !bc_is_zero(parity) )
+ {
+ bc_multiply (temp, power, &temp, rscale);
+ (void) bc_modulo (temp, mod, &temp, scale);
+ }
+
+ bc_multiply (power, power, &power, rscale);
+ (void) bc_modulo (power, mod, &power, scale);
}
-
- bc_multiply (power, power, &power, rscale);
- (void) bc_modulo (power, mod, &power, scale);
}
/* Assign the value. */
diff --git a/ext/bcmath/tests/bug54598.phpt b/ext/bcmath/tests/bug54598.phpt
new file mode 100644
index 0000000..6ccd61a
--- /dev/null
+++ b/ext/bcmath/tests/bug54598.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Bug #54598 (bcpowmod() may return 1 if modulus is 1)
+--SKIPIF--
+<?php
+if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
+?>
+--FILE--
+<?php
+var_dump(bcpowmod(5, 0, 1));
+var_dump(bcpowmod(5, 0, 1, 3));
+?>
+===DONE===
+--EXPECT--
+string(1) "0"
+string(5) "0.000"
+===DONE===