Skip to content

Commit 2c97b40

Browse files
vv12131415kocsismate
authored andcommitted
make bcpowmod stricter by not returning false, instead throw exception
Closes GH-5747
1 parent ec80b78 commit 2c97b40

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

ext/bcmath/bcmath.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,16 @@ PHP_FUNCTION(bcpowmod)
404404
php_str2num(&second, ZSTR_VAL(right));
405405
php_str2num(&mod, ZSTR_VAL(modulus));
406406

407-
if (bc_raisemod(first, second, mod, &result, scale) != -1) {
408-
RETVAL_STR(bc_num2str_ex(result, scale));
409-
} else {
410-
RETVAL_FALSE;
407+
switch (bc_raisemod(first, second, mod, &result, scale)) {
408+
case 0:
409+
RETVAL_STR(bc_num2str_ex(result, scale));
410+
break;
411+
case -1:
412+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
413+
break;
414+
case -2:
415+
zend_argument_value_error(2, "must be greater than 0");
416+
break;
411417
}
412418

413419
bc_free_num(&first);

ext/bcmath/bcmath.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function bcdiv(string $dividend, string $divisor, ?int $scale = null): string {}
1212

1313
function bcmod(string $dividend, string $divisor, ?int $scale = null): string {}
1414

15-
function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string|false {}
15+
function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string {}
1616

1717
function bcpow(string $base, string $exponent, ?int $scale = null): string {}
1818

ext/bcmath/bcmath_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ZEND_END_ARG_INFO()
1818

1919
#define arginfo_bcmod arginfo_bcdiv
2020

21-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bcpowmod, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
21+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpowmod, 0, 3, IS_STRING, 0)
2222
ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0)
2323
ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0)
2424
ZEND_ARG_TYPE_INFO(0, modulus, IS_STRING, 0)

ext/bcmath/libbcmath/src/raisemod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
6767

6868
/* Check for correct numbers. */
6969
if (bc_is_zero(mod)) return -1;
70-
if (bc_is_neg(expo)) return -1;
70+
if (bc_is_neg(expo)) return -2;
7171

7272
/* Set initial values. */
7373
power = bc_copy_num (base);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
bc_raisemod's expo can't be negative
3+
--CREDITS--
4+
Gabriel Caruso (carusogabriel34@gmail.com)
5+
--SKIPIF--
6+
<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
7+
--FILE--
8+
<?php
9+
try {
10+
var_dump(bcpowmod('1', '-1', '2'));
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . \PHP_EOL;
13+
}
14+
?>
15+
--EXPECT--
16+
bcpowmod(): Argument #2 ($exponent) must be greater than 0
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
--TEST--
2-
bc_raisemod's mod can't be zero and expo can't be negative
2+
bc_raisemod's mod can't be zero
33
--CREDITS--
44
Gabriel Caruso (carusogabriel34@gmail.com)
55
--SKIPIF--
66
<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
77
--FILE--
8-
<?php var_dump(bcpowmod('1', '-1', '0')); ?>
8+
<?php
9+
try {
10+
var_dump(bcpowmod('1', '-1', '0'));
11+
} catch (DivisionByZeroError $ex) {
12+
echo $ex->getMessage(), PHP_EOL;
13+
}
14+
?>
915
--EXPECT--
10-
bool(false)
16+
Modulo by zero

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ static const func_info_t func_infos[] = {
789789
F1("bcmul", MAY_BE_STRING),
790790
F1("bcdiv", MAY_BE_STRING),
791791
F1("bcmod", MAY_BE_STRING),
792-
F1("bcpowmod", MAY_BE_FALSE | MAY_BE_STRING),
792+
F1("bcpowmod", MAY_BE_STRING),
793793
F1("bcpow", MAY_BE_STRING),
794794
F1("bcsqrt", MAY_BE_STRING),
795795

0 commit comments

Comments
 (0)