[php-src] PR #16884: Fix GH-16870: gmp_pow(64, 11) throws overflow exception
| From: | cmb69 | Date: | Thu, 21 Nov 2024 11:22:53 +0000 |
| Subject: | [php-src] PR #16884: Fix GH-16870: gmp_pow(64, 11) throws overflow exception | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-29718@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/16884
Author: cmb69
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.
---
Note that the threshold of 2000 seems *way* too small. To avoid triggering overflow handling during
allocations it should rather be close to SIZE_MAX (or whatever libgmp/mpir assume
there), but even 10,000 would cause
https://github.com/php/php-src/blob/ff3b4eca0e39dc8898db0acfb1e751673ea87ee2/ext/gmp/tests/gmp_pow_fpe.phpt#L20-L29
to no longer throw, but instead yield reasonable results: numbers with a bit less than 10,000 and
5,000 digits, respectively. It's hard for me to tell whether these examples would actually
cause libgmp's overflow check to trigger, since mpir doesn't even have these overflow
checks.
In any way, it might not be the best idea to push the limits of our early overflow check just to
avoid overflow checks to be triggered by libgmp, since we're likely hitting OOM, which causes
abort.