Skip to content

make bcpowmod stricter by not returning false, instead throw exception #5747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions ext/bcmath/bcmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,16 @@ PHP_FUNCTION(bcpowmod)
php_str2num(&second, ZSTR_VAL(right));
php_str2num(&mod, ZSTR_VAL(modulus));

if (bc_raisemod(first, second, mod, &result, scale) != -1) {
RETVAL_STR(bc_num2str_ex(result, scale));
} else {
RETVAL_FALSE;
switch (bc_raisemod(first, second, mod, &result, scale)) {
case 0:
RETVAL_STR(bc_num2str_ex(result, scale));
break;
case -1:
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
break;
case -2:
zend_argument_value_error(2, "must be greater than 0");
break;
}

bc_free_num(&first);
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/bcmath.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function bcdiv(string $dividend, string $divisor, ?int $scale = null): string {}

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/bcmath_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ZEND_END_ARG_INFO()

#define arginfo_bcmod arginfo_bcdiv

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bcpowmod, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpowmod, 0, 3, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, modulus, IS_STRING, 0)
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/raisemod.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)

/* Check for correct numbers. */
if (bc_is_zero(mod)) return -1;
if (bc_is_neg(expo)) return -1;
if (bc_is_neg(expo)) return -2;

/* Set initial values. */
power = bc_copy_num (base);
Expand Down
16 changes: 16 additions & 0 deletions ext/bcmath/tests/bcpowmod_negative_exponent.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
bc_raisemod's expo can't be negative
--CREDITS--
Gabriel Caruso ([email protected])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, wait, what my name is doing here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, too late, sorry :S You can remove it from master though :)

--SKIPIF--
<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
--FILE--
<?php
try {
var_dump(bcpowmod('1', '-1', '2'));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
bcpowmod(): Argument #2 ($exponent) must be greater than 0
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
--TEST--
bc_raisemod's mod can't be zero and expo can't be negative
bc_raisemod's mod can't be zero
--CREDITS--
Gabriel Caruso ([email protected])
--SKIPIF--
<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
--FILE--
<?php var_dump(bcpowmod('1', '-1', '0')); ?>
<?php
try {
var_dump(bcpowmod('1', '-1', '0'));
} catch (DivisionByZeroError $ex) {
echo $ex->getMessage(), PHP_EOL;
}
?>
--EXPECT--
bool(false)
Modulo by zero
2 changes: 1 addition & 1 deletion ext/opcache/Optimizer/zend_func_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ static const func_info_t func_infos[] = {
F1("bcmul", MAY_BE_STRING),
F1("bcdiv", MAY_BE_STRING),
F1("bcmod", MAY_BE_STRING),
F1("bcpowmod", MAY_BE_FALSE | MAY_BE_STRING),
F1("bcpowmod", MAY_BE_STRING),
F1("bcpow", MAY_BE_STRING),
F1("bcsqrt", MAY_BE_STRING),

Expand Down