-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/gmp: gmp_pow fix FPE with large values. #16384
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c04ae6
ext/gmp: gmp_pow fix FPE with large values.
devnexen e2b07d7
add test
devnexen 3534f0a
add tests
devnexen 72b5653
change comparison limit, now new tests overflow on 64 bits.
devnexen 7f432a1
zend_ulong
devnexen 752627d
fix tests on 32 bits attempt.
devnexen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--TEST-- | ||
gmp_pow() basic tests | ||
--EXTENSIONS-- | ||
gmp | ||
--SKIPIF-- | ||
<?php if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only"); ?> | ||
--FILE-- | ||
<?php | ||
|
||
var_dump(gmp_strval(gmp_pow(2,10))); | ||
var_dump(gmp_strval(gmp_pow(-2,10))); | ||
var_dump(gmp_strval(gmp_pow(-2,11))); | ||
var_dump(gmp_strval(gmp_pow("2",10))); | ||
var_dump(gmp_strval(gmp_pow("2",0))); | ||
try { | ||
gmp_pow("2", -1); | ||
} catch (ValueError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
var_dump(gmp_strval(gmp_pow("-2",10))); | ||
try { | ||
gmp_pow(20,10); | ||
} catch (ValueError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
try { | ||
gmp_pow(50,10); | ||
} catch (ValueError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
try { | ||
gmp_pow(50,-5); | ||
} catch (ValueError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
try { | ||
$n = gmp_init("20"); | ||
gmp_pow($n,10); | ||
} catch (ValueError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
try { | ||
$n = gmp_init("-20"); | ||
gmp_pow($n,10); | ||
} catch (ValueError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
try { | ||
var_dump(gmp_pow(2,array())); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
var_dump(gmp_pow(array(),10)); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage() . \PHP_EOL; | ||
} | ||
|
||
echo "Done\n"; | ||
?> | ||
--EXPECT-- | ||
string(4) "1024" | ||
string(4) "1024" | ||
string(5) "-2048" | ||
string(4) "1024" | ||
string(1) "1" | ||
gmp_pow(): Argument #2 ($exponent) must be greater than or equal to 0 | ||
string(4) "1024" | ||
base and exponent overflow | ||
base and exponent overflow | ||
gmp_pow(): Argument #2 ($exponent) must be greater than or equal to 0 | ||
base and exponent overflow | ||
base and exponent overflow | ||
gmp_pow(): Argument #2 ($exponent) must be of type int, array given | ||
gmp_pow(): Argument #1 ($num) must be of type GMP|string|int, array given | ||
Done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can overflow; e.g.
gmp_pow(gmp_init(PHP_INT_MAX), 256)
is sufficient for overflow on x64 Windows; on LP64,gmp_pow(gmp_add(gmp_mul(gmp_init(PHP_INT_MAX), gmp_init(PHP_INT_MAX)), 3), 256)
is likely to overflow here.It seems to me that the libgmp/mpir developers where so focused to make it fast, that they forgot to make it right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ll add those two cases, the latter does not crash on linux/64 locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that
mpz_get_ui()
returnsunsigned long long int
(at least on Windows) which is an unsigned 64bit value. If you assign that to anunsigned long
, the value will be truncated (well-defined behavior, but likely not desired here). It might be better to declarempir_ui gmpnum
, and then go from there, but that might cause further issues down the line.