Skip to content

Commit 0236667

Browse files
committed
ext/gmp: gmp_fact() reject values larger than unsigned long.
close GH-21794
1 parent 2945c72 commit 0236667

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ PHP NEWS
3636
. imagesetstyle()/imagefilter()/imagecrop() check array argument entries
3737
types. (David Carlier)
3838

39+
- GMP:
40+
. gmp_fact() reject values larger than unsigned long. (David Carlier)
41+
3942
- Hash:
4043
. Upgrade xxHash to 0.8.2. (timwolla)
4144

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ PHP 8.6 UPGRADE NOTES
141141
5. Changed Functions
142142
========================================
143143

144+
- GMP:
145+
. gmp_fact() now throws a ValueError() if $num does not fit into
146+
a unsigned long.
147+
144148
- mysqli:
145149
. The return structure of mysqli_get_charset() no longer contains
146150
the undocumented "comment" element. The value of "charsetnr" is

ext/gmp/gmp.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,10 @@ ZEND_FUNCTION(gmp_fact)
10921092
RETURN_THROWS();
10931093
}
10941094

1095-
// TODO: Check that we don't an int that is larger than an unsigned long?
1096-
// Could use mpz_fits_slong_p() if we revert to using mpz_get_si()
1095+
if (!mpz_fits_ulong_p(gmpnum)) {
1096+
zend_argument_value_error(1, "must be between 0 and %lu", ULONG_MAX);
1097+
RETURN_THROWS();
1098+
}
10971099

10981100
INIT_GMP_RETVAL(gmpnum_result);
10991101
mpz_fac_ui(gmpnum_result, mpz_get_ui(gmpnum));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
gmp_fact() rejects values larger than unsigned long
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
8+
try {
9+
var_dump(gmp_fact(gmp_pow(2, 100)));
10+
} catch (\ValueError $e) {
11+
echo $e->getMessage() . \PHP_EOL;
12+
}
13+
14+
try {
15+
var_dump(gmp_fact(gmp_init("18446744073709551616")));
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . \PHP_EOL;
18+
}
19+
20+
echo "Done\n";
21+
?>
22+
--EXPECTF--
23+
gmp_fact(): Argument #1 ($num) must be between 0 and %d
24+
gmp_fact(): Argument #1 ($num) must be between 0 and %d
25+
Done

0 commit comments

Comments
 (0)