Skip to content

Commit 3c08be9

Browse files
committed
Warning to Error promotion in bcpowmod()
1 parent d2ddd3c commit 3c08be9

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

ext/bcmath/bcmath.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ PHP_FUNCTION(bcpowmod)
403403
case -2:
404404
zend_argument_value_error(2, "must be greater than 0");
405405
break;
406+
case -3:
407+
/* ValueError thrown in bc_raisemod() */
408+
break;
406409
}
407410

408411
bc_free_num(&first);

ext/bcmath/libbcmath/src/raisemod.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
6868
/* Check for correct numbers. */
6969
if (bc_is_zero(mod)) return -1;
7070
if (bc_is_neg(expo)) return -2;
71+
/* Check the base for scale digits. */
72+
if (base->n_scale != 0) {
73+
/* 1st argument from PHP_FUNCTION(bcpowmod) */
74+
zend_argument_value_error(1, "must be an integer");
75+
return -3;
76+
}
77+
/* Check the exponent for scale digits. */
78+
if (expo->n_scale != 0) {
79+
/* 2nd argument from PHP_FUNCTION(bcpowmod) */
80+
zend_argument_value_error(2, "must be an integer");
81+
return -3;
82+
}
83+
/* Check the modulus for scale digits. */
84+
if (mod->n_scale != 0) {
85+
/* 3rd argument from PHP_FUNCTION(bcpowmod) */
86+
zend_argument_value_error(3, "must be an integer");
87+
return -3;
88+
}
7189

7290
/* Set initial values. */
7391
power = bc_copy_num (base);
@@ -76,27 +94,6 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
7694
temp = bc_copy_num (BCG(_one_));
7795
bc_init_num(&parity);
7896

79-
/* Check the base for scale digits. */
80-
if (power->n_scale != 0)
81-
{
82-
php_error_docref (NULL, E_WARNING, "Non-zero scale in base");
83-
_bc_truncate (&power);
84-
}
85-
86-
/* Check the exponent for scale digits. */
87-
if (exponent->n_scale != 0)
88-
{
89-
php_error_docref (NULL, E_WARNING, "Non-zero scale in exponent");
90-
_bc_truncate (&exponent);
91-
}
92-
93-
/* Check the modulus for scale digits. */
94-
if (modulus->n_scale != 0)
95-
{
96-
php_error_docref (NULL, E_WARNING, "Non-zero scale in modulus");
97-
_bc_truncate (&modulus);
98-
}
99-
10097
/* Do the calculation. */
10198
rscale = MAX(scale, power->n_scale);
10299
if ( !bc_compare(modulus, BCG(_one_)) )

ext/bcmath/tests/bug72093.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ try {
1111
} catch (\ValueError $e) {
1212
echo $e->getMessage() . \PHP_EOL;
1313
}
14-
var_dump(bcpowmod(1, 1.2, 1, 1));
14+
try {
15+
var_dump(bcpowmod(1, 1.2, 1, 1));
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . \PHP_EOL;
18+
}
1519
?>
16-
--EXPECTF--
20+
--EXPECT--
1721
bcpowmod(): Argument #4 ($scale) must be between 0 and 2147483647
18-
19-
Warning: bcpowmod(): Non-zero scale in exponent in %s on line %d
20-
string(3) "0.0"
22+
bcpowmod(): Argument #2 ($exponent) must be an integer

ext/bcmath/tests/bug75178.phpt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
66
?>
77
--FILE--
88
<?php
9-
var_dump(bcpowmod('4.1', '4', '3', 3));
10-
var_dump(bcpowmod('4', '4', '3.1', 3));
9+
try {
10+
var_dump(bcpowmod('4.1', '4', '3', 3));
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . \PHP_EOL;
13+
}
14+
try {
15+
var_dump(bcpowmod('4', '4', '3.1', 3));
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . \PHP_EOL;
18+
}
1119
?>
12-
--EXPECTF--
13-
Warning: bcpowmod(): Non-zero scale in base in %s on line %d
14-
string(5) "1.000"
15-
16-
Warning: bcpowmod(): Non-zero scale in modulus in %s on line %d
17-
string(5) "1.000"
20+
--EXPECT--
21+
bcpowmod(): Argument #1 ($base) must be an integer
22+
bcpowmod(): Argument #3 ($modulus) must be an integer

ext/bcmath/tests/bug78878.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
66
?>
77
--FILE--
88
<?php
9-
print @bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4));
9+
try {
10+
print bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4));
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . \PHP_EOL;
13+
}
1014
?>
1115
--EXPECT--
12-
0
16+
bcpowmod(): Argument #3 ($modulus) must be an integer

0 commit comments

Comments
 (0)