-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-16878: gmp_fact overflow on memory allocation attempt. #16880
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
base: PHP-8.2
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1281,20 +1281,28 @@ ZEND_FUNCTION(gmp_fact) | |
RETURN_THROWS(); | ||
} | ||
|
||
#if SIZEOF_SIZE_T == 4 | ||
const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS; | ||
#else | ||
const zend_long maxbits = INT_MAX; | ||
#endif | ||
|
||
|
||
if (Z_TYPE_P(a_arg) == IS_LONG) { | ||
if (Z_LVAL_P(a_arg) < 0) { | ||
zend_argument_value_error(1, "must be greater than or equal to 0"); | ||
if (Z_LVAL_P(a_arg) < 0 || Z_LVAL_P(a_arg) > maxbits) { | ||
zend_argument_value_error(1, "must be between 0 and " ZEND_LONG_FMT, maxbits); | ||
RETURN_THROWS(); | ||
} | ||
} else { | ||
mpz_ptr gmpnum; | ||
gmp_temp_t temp_a; | ||
|
||
FETCH_GMP_ZVAL(gmpnum, a_arg, temp_a, 1); | ||
long r = mpz_get_si(gmpnum); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
FREE_GMP_TEMP(temp_a); | ||
|
||
if (mpz_sgn(gmpnum) < 0) { | ||
zend_argument_value_error(1, "must be greater than or equal to 0"); | ||
if (r < 0 || r > maxbits) { | ||
zend_argument_value_error(1, "must be between 0 and " ZEND_LONG_FMT, maxbits); | ||
RETURN_THROWS(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
GH-16878 (gmp_fact overflow) | ||
--EXTENSIONS-- | ||
gmp | ||
--FILE-- | ||
<?php | ||
try { | ||
gmp_fact(PHP_INT_MAX); | ||
} catch (\ValueError $e) { | ||
echo $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
try { | ||
gmp_fact(gmp_init(PHP_INT_MAX)); | ||
} catch (\ValueError $e) { | ||
echo $e->getMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
gmp_fact(): Argument #1 ($num) must be between 0 and %d | ||
gmp_fact(): Argument #1 ($num) must be between 0 and %d |
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 should be moved to the top-level, since we're already using it in
gmp_random_bits()
, and may want to re-use it elsewhere. (Could also be a macro; doesn't really matter, I think.)