Skip to content

Commit d9f0e36

Browse files
committed
Refactor ValueError handling for bcpowmod()
1 parent 503ae69 commit d9f0e36

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

ext/bcmath/bcmath.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,8 @@ PHP_FUNCTION(bcpowmod)
393393
php_str2num(&second, ZSTR_VAL(right));
394394
php_str2num(&mod, ZSTR_VAL(modulus));
395395

396-
switch (bc_raisemod(first, second, mod, &result, scale)) {
397-
case 0:
398-
RETVAL_STR(bc_num2str_ex(result, scale));
399-
break;
400-
case -1:
401-
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
402-
break;
403-
case -2:
404-
zend_argument_value_error(2, "must be greater than 0");
405-
break;
406-
case -3:
407-
/* ValueError thrown in bc_raisemod() */
408-
break;
396+
if (bc_raisemod(first, second, mod, &result, scale) == SUCCESS) {
397+
RETVAL_STR(bc_num2str_ex(result, scale));
409398
}
410399

411400
bc_free_num(&first);

ext/bcmath/libbcmath/src/raisemod.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,41 @@
3636
#include <stdarg.h>
3737
#include "bcmath.h"
3838
#include "private.h"
39+
#include "zend_exceptions.h"
3940

4041
/* Raise BASE to the EXPO power, reduced modulo MOD. The result is placed in RESULT. */
41-
int bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
42+
zend_result bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
4243
{
4344
bc_num power, exponent, modulus, parity, temp;
4445
int rscale;
4546

46-
/* Check for correct numbers. */
47-
if (bc_is_zero(mod)) return -1;
48-
if (bc_is_neg(expo)) return -2;
4947
/* Check the base for scale digits. */
5048
if (base->n_scale != 0) {
5149
/* 1st argument from PHP_FUNCTION(bcpowmod) */
5250
zend_argument_value_error(1, "must be an integer");
53-
return -3;
51+
return FAILURE;
5452
}
5553
/* Check the exponent for scale digits. */
5654
if (expo->n_scale != 0) {
5755
/* 2nd argument from PHP_FUNCTION(bcpowmod) */
5856
zend_argument_value_error(2, "must be an integer");
59-
return -3;
57+
return FAILURE;
6058
}
59+
if (bc_is_neg(expo)) {
60+
zend_argument_value_error(2, "must be greater than or equal to 0");
61+
return FAILURE;
62+
}
6163
/* Check the modulus for scale digits. */
6264
if (mod->n_scale != 0) {
6365
/* 3rd argument from PHP_FUNCTION(bcpowmod) */
6466
zend_argument_value_error(3, "must be an integer");
65-
return -3;
67+
return FAILURE;
6668
}
69+
/* Modulus cannot be 0 */
70+
if (bc_is_zero(mod)) {
71+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
72+
return FAILURE;
73+
}
6774

6875
/* Set initial values. */
6976
power = bc_copy_num (base);
@@ -102,5 +109,5 @@ int bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale
102109
bc_free_num (result);
103110
bc_free_num (&parity);
104111
*result = temp;
105-
return 0; /* Everything is OK. */
112+
return SUCCESS; /* Everything is OK. */
106113
}

ext/bcmath/tests/bcpowmod.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ bcpowmod() - Raise an arbitrary precision number to another, reduced by a specif
66
bcmath.scale=0
77
--FILE--
88
<?php
9-
echo bcpowmod("5", "2", "7") . "\n";
10-
echo bcpowmod("-2", "5", "7") . "\n";
11-
echo bcpowmod("10", "2147483648", "2047");
9+
var_dump(bcpowmod("5", "2", "7"));
10+
var_dump(bcpowmod("-2", "5", "7"));
11+
var_dump(bcpowmod("10", "2147483648", "2047"));
12+
var_dump(bcpowmod("10", "0", "2047"));
1213
?>
1314
--EXPECT--
14-
4
15-
-4
16-
790
15+
string(1) "4"
16+
string(2) "-4"
17+
string(3) "790"
18+
string(1) "1"

ext/bcmath/tests/bcpowmod_negative_exponent.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ try {
1313
}
1414
?>
1515
--EXPECT--
16-
bcpowmod(): Argument #2 ($exponent) must be greater than 0
16+
bcpowmod(): Argument #2 ($exponent) must be greater than or equal to 0

ext/bcmath/tests/bcpowmod_zero_modulus.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gabriel Caruso (carusogabriel34@gmail.com)
77
--FILE--
88
<?php
99
try {
10-
var_dump(bcpowmod('1', '-1', '0'));
10+
var_dump(bcpowmod('1', '1', '0'));
1111
} catch (DivisionByZeroError $ex) {
1212
echo $ex->getMessage(), PHP_EOL;
1313
}

0 commit comments

Comments
 (0)