Skip to content

Commit c025ce4

Browse files
committed
Merge branch 'PHP-8.4'
2 parents 01aeaf2 + bde23d0 commit c025ce4

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

ext/gmp/gmp.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,15 +1821,21 @@ ZEND_FUNCTION(gmp_random_bits)
18211821
RETURN_THROWS();
18221822
}
18231823

1824-
if (bits <= 0) {
1825-
zend_argument_value_error(1, "must be greater than or equal to 1");
1824+
#if SIZEOF_SIZE_T == 4
1825+
const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS;
1826+
#else
1827+
const zend_long maxbits = INT_MAX;
1828+
#endif
1829+
1830+
if (bits <= 0 || bits > maxbits) {
1831+
zend_argument_value_error(1, "must be between 1 and " ZEND_LONG_FMT, maxbits);
18261832
RETURN_THROWS();
18271833
}
18281834

18291835
INIT_GMP_RETVAL(gmpnum_result);
18301836
gmp_init_random();
18311837

1832-
mpz_urandomb(gmpnum_result, GMPG(rand_state), bits);
1838+
mpz_urandomb(gmpnum_result, GMPG(rand_state), (mp_bitcnt_t)bits);
18331839
}
18341840
/* }}} */
18351841

ext/gmp/tests/gh16501.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-16501 (gmp_random_bits overflow)
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
try {
8+
gmp_random_bits(PHP_INT_MAX);
9+
} catch (\ValueError $e) {
10+
echo $e->getMessage();
11+
}
12+
?>
13+
--EXPECTF--
14+
gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d

ext/gmp/tests/gmp_random_bits.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ while (1) {
4040

4141
echo "Done\n";
4242
?>
43-
--EXPECT--
44-
gmp_random_bits(): Argument #1 ($bits) must be greater than or equal to 1
45-
gmp_random_bits(): Argument #1 ($bits) must be greater than or equal to 1
43+
--EXPECTF--
44+
gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
45+
gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
4646
Done

0 commit comments

Comments
 (0)