Skip to content

Commit

Permalink
change comparison limit, now new tests overflow on 64 bits.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Oct 24, 2024
1 parent 3534f0a commit 72b5653
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 5 additions & 3 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1282,20 +1282,22 @@ ZEND_FUNCTION(gmp_pow)
RETURN_THROWS();
}

double powmax = log((double)ZEND_LONG_MAX);

if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
INIT_GMP_RETVAL(gmpnum_result);
if ((log10(Z_LVAL_P(base_arg)) * exp) > (double)ULONG_MAX) {
if ((log(Z_LVAL_P(base_arg)) * exp) > powmax) {
zend_value_error("base and exponent overflow");
RETURN_THROWS();
}
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
} else {
mpz_ptr gmpnum_base;
unsigned long gmpnum;
unsigned long int gmpnum;
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
INIT_GMP_RETVAL(gmpnum_result);
gmpnum = mpz_get_ui(gmpnum_base);
if ((log10(gmpnum) * exp) > (double)ULONG_MAX) {
if ((log(gmpnum) * exp) > powmax) {
FREE_GMP_TEMP(temp_base);
zend_value_error("base and exponent overflow");
RETURN_THROWS();
Expand Down
22 changes: 12 additions & 10 deletions ext/gmp/tests/gmp_pow_fpe.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ try {
echo $e->getMessage() . PHP_EOL;
}

var_dump(gmp_pow(gmp_add(gmp_mul(gmp_init(PHP_INT_MAX), gmp_init(PHP_INT_MAX)), 3), 256));
var_dump(gmp_pow(gmp_init(PHP_INT_MAX), 256));
try {
gmp_pow(gmp_add(gmp_mul(gmp_init(PHP_INT_MAX), gmp_init(PHP_INT_MAX)), 3), 256);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
gmp_pow(gmp_init(PHP_INT_MAX), 256);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
base and exponent overflow
base and exponent overflow
object(GMP)#%d (1) {
["num"]=>
string(%d) "%s"
}
object(GMP)#%d (1) {
["num"]=>
string(%d) "%s"
}
base and exponent overflow
base and exponent overflow

0 comments on commit 72b5653

Please sign in to comment.