Skip to content

Commit b805d4a

Browse files
committed
Use mpz_sizeinbase(gmpnum_base, 2) - 1
That is equivalent to `floor(log2(Z_LVAL_P(base_arg)))` and as such more suitable[1]. Since this now fails gmp_pow_fpe.phpt, we use an int there. [1] <php#16884 (comment)>
1 parent 9a270e0 commit b805d4a

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

ext/gmp/gmp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ ZEND_FUNCTION(gmp_pow)
13631363
mpz_ptr gmpnum_base;
13641364
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
13651365
INIT_GMP_RETVAL(gmpnum_result);
1366-
if ((mpz_sizeinbase(gmpnum_base, 2) * exp) > max_bits) {
1366+
if (((mpz_sizeinbase(gmpnum_base, 2) - 1) * exp) > max_bits) {
13671367
FREE_GMP_TEMP(temp_base);
13681368
zend_value_error("base and exponent overflow");
13691369
RETURN_THROWS();

ext/gmp/tests/gmp_pow_fpe.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ try {
2323
echo $e->getMessage() . PHP_EOL;
2424
}
2525
try {
26-
gmp_pow(gmp_init(PHP_INT_MAX), 256);
26+
gmp_pow(PHP_INT_MAX, 256);
2727
} catch (\ValueError $e) {
2828
echo $e->getMessage();
2929
}

0 commit comments

Comments
 (0)