Skip to content

Commit 0f893ea

Browse files
committed
change comparison limit, now new tests overflow on 64 bits.
1 parent 3534f0a commit 0f893ea

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

ext/gmp/gmp.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -1282,20 +1282,22 @@ ZEND_FUNCTION(gmp_pow)
12821282
RETURN_THROWS();
12831283
}
12841284

1285+
double powmax = log((double)ZEND_LONG_MAX);
1286+
12851287
if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
12861288
INIT_GMP_RETVAL(gmpnum_result);
1287-
if ((log10(Z_LVAL_P(base_arg)) * exp) > (double)ULONG_MAX) {
1289+
if ((log(Z_LVAL_P(base_arg)) * exp) > powmax) {
12881290
zend_value_error("base and exponent overflow");
12891291
RETURN_THROWS();
12901292
}
12911293
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
12921294
} else {
12931295
mpz_ptr gmpnum_base;
1294-
unsigned long gmpnum;
1296+
unsigned long int gmpnum;
12951297
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
12961298
INIT_GMP_RETVAL(gmpnum_result);
12971299
gmpnum = mpz_get_ui(gmpnum_base);
1298-
if ((log10(gmpnum) * exp) > (double)ULONG_MAX) {
1300+
if ((log(gmpnum) * exp) > powmax) {
12991301
FREE_GMP_TEMP(temp_base);
13001302
zend_value_error("base and exponent overflow");
13011303
RETURN_THROWS();

ext/gmp/tests/gmp_pow_fpe.phpt

+12-10
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ try {
1717
echo $e->getMessage() . PHP_EOL;
1818
}
1919

20-
var_dump(gmp_pow(gmp_add(gmp_mul(gmp_init(PHP_INT_MAX), gmp_init(PHP_INT_MAX)), 3), 256));
21-
var_dump(gmp_pow(gmp_init(PHP_INT_MAX), 256));
20+
try {
21+
gmp_pow(gmp_add(gmp_mul(gmp_init(PHP_INT_MAX), gmp_init(PHP_INT_MAX)), 3), 256);
22+
} catch (\ValueError $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
25+
try {
26+
gmp_pow(gmp_init(PHP_INT_MAX), 256));
27+
} catch (\ValueError $e) {
28+
echo $e->getMessage();
29+
}
2230
?>
2331
--EXPECTF--
2432
base and exponent overflow
2533
base and exponent overflow
26-
object(GMP)#%d (1) {
27-
["num"]=>
28-
string(%d) "%s"
29-
}
30-
object(GMP)#%d (1) {
31-
["num"]=>
32-
string(%d) "%s"
33-
}
34+
base and exponent overflow
35+
base and exponent overflow

0 commit comments

Comments
 (0)