Skip to content

Commit d01453b

Browse files
committed
Merge branch 'PHP-7.1' into PHP-7.2
* PHP-7.1: Fixed bug #54598 (bcpowmod() may return 1 if modulus is 1)
2 parents 5ce7440 + 0f88a49 commit d01453b

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PHP NEWS
88

99
- BCMath:
1010
. Fixed bug #44995 (bcpowmod() fails if scale != 0). (cmb)
11+
. Fixed bug #54598 (bcpowmod() may return 1 if modulus is 1). (okano1220, cmb)
1112

1213
- CLI server:
1314
. Fixed bug #70470 (Built-in server truncates headers spanning over TCP

ext/bcmath/libbcmath/src/raisemod.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,24 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
7575

7676
/* Do the calculation. */
7777
rscale = MAX(scale, base->n_scale);
78-
while ( !bc_is_zero(exponent) )
78+
if ( !bc_compare(mod, BCG(_one_)) )
7979
{
80-
(void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
81-
if ( !bc_is_zero(parity) )
80+
temp = bc_new_num (1, scale);
81+
}
82+
else
83+
{
84+
while ( !bc_is_zero(exponent) )
8285
{
83-
bc_multiply (temp, power, &temp, rscale);
84-
(void) bc_modulo (temp, mod, &temp, scale);
86+
(void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
87+
if ( !bc_is_zero(parity) )
88+
{
89+
bc_multiply (temp, power, &temp, rscale);
90+
(void) bc_modulo (temp, mod, &temp, scale);
91+
}
92+
93+
bc_multiply (power, power, &power, rscale);
94+
(void) bc_modulo (power, mod, &power, scale);
8595
}
86-
87-
bc_multiply (power, power, &power, rscale);
88-
(void) bc_modulo (power, mod, &power, scale);
8996
}
9097

9198
/* Assign the value. */

ext/bcmath/tests/bug54598.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #54598 (bcpowmod() may return 1 if modulus is 1)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(bcpowmod(5, 0, 1));
10+
var_dump(bcpowmod(5, 0, 1, 3));
11+
?>
12+
===DONE===
13+
--EXPECT--
14+
string(1) "0"
15+
string(5) "0.000"
16+
===DONE===

0 commit comments

Comments
 (0)