Skip to content

Fix GH-16870: gmp_pow(64, 11) throws overflow exception #16884

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix GH-16870: gmp_pow(64, 11) throws overflow exception
The current guard to prevent FPEs is way too restrictive; `64 ** 11` is
a perfectly reasonable operation.  Instead, we now estimate the number
of bytes of the resulting GMP (assuming that numbers are stored base
256 encoded), and fail if that exceeds a given threshold.  The chosen
threshold is somewhat arbitrary.

We also ensure that we do not prematurely convert a given non int base
to an int to avoid overflow which could circumvent our early check.
  • Loading branch information
cmb69 committed Nov 25, 2024
commit d5ed93b1186d33f261b40189c7932863a4ee3c12
7 changes: 3 additions & 4 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1350,11 +1350,11 @@ ZEND_FUNCTION(gmp_pow)
RETURN_THROWS();
}

double powmax = log((double)ZEND_LONG_MAX);
double powmax = 2000;

if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
INIT_GMP_RETVAL(gmpnum_result);
if ((log(Z_LVAL_P(base_arg)) * exp) > powmax) {
if ((log(Z_LVAL_P(base_arg)) / log(256) * exp) > powmax) {
Copy link
Member

Choose a reason for hiding this comment

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

Don't we just care if it can be "allocated reasonably" ?
Which is actually somewhat described in section 5.14 of the GMP manual (mpz_export)
image

Wonder if we shouldn't use a heuristic based on that?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is basically what is already happening in this patch. The difference is that I'm counting bytes, not bits. The logic is to estimate the number of bytes the result would have, and if this is greater than some threshold (powmax is badly named; should be rather max_size_in_bytes or so), trigger a value error.

mpz_sizeinbase() is only used in the else branch, because if the input is a zend_long, that wouldn't work. So I'm calculating log256(base_arg) * exp instead.

I'll update to counting bytes.

zend_value_error("base and exponent overflow");
RETURN_THROWS();
}
Expand All @@ -1364,8 +1364,7 @@ ZEND_FUNCTION(gmp_pow)
zend_ulong gmpnum;
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
INIT_GMP_RETVAL(gmpnum_result);
gmpnum = mpz_get_ui(gmpnum_base);
if ((log(gmpnum) * exp) > powmax) {
if ((mpz_sizeinbase(gmpnum_base, 16) / 2.0 * exp) > powmax) {
FREE_GMP_TEMP(temp_base);
zend_value_error("base and exponent overflow");
RETURN_THROWS();
Expand Down
16 changes: 16 additions & 0 deletions ext/gmp/tests/gh16870.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
GH-16870 (gmp_pow(64, 11) throws overflow exception)
--EXTENSIONS--
gmp
--FILE--
<?php
var_dump((string) gmp_pow(64, 11));
try {
gmp_pow("18446744073709551616", 0x7fffffff);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
string(20) "73786976294838206464"
base and exponent overflow