Skip to content

Commit d6bac2f

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
2 parents ce44a7f + 9ca68e0 commit d6bac2f

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ PHP NEWS
4747
. Fixed floating point exception bug with gmp_pow when using
4848
large exposant values. (David Carlier).
4949
. Fixed bug GH-16411 (gmp_export() can cause overflow). (cmb)
50+
. Fixed bug GH-16501 (gmp_random_bits() can cause overflow).
51+
(David Carlier)
5052

5153
- MBstring:
5254
. Fixed bug GH-16361 (mb_substr overflow on start/length arguments).

ext/gmp/gmp.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,15 +1807,21 @@ ZEND_FUNCTION(gmp_random_bits)
18071807
RETURN_THROWS();
18081808
}
18091809

1810-
if (bits <= 0) {
1811-
zend_argument_value_error(1, "must be greater than or equal to 1");
1810+
#if SIZEOF_SIZE_T == 4
1811+
const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS;
1812+
#else
1813+
const zend_long maxbits = INT_MAX;
1814+
#endif
1815+
1816+
if (bits <= 0 || bits > maxbits) {
1817+
zend_argument_value_error(1, "must be between 1 and " ZEND_LONG_FMT, maxbits);
18121818
RETURN_THROWS();
18131819
}
18141820

18151821
INIT_GMP_RETVAL(gmpnum_result);
18161822
gmp_init_random();
18171823

1818-
mpz_urandomb(gmpnum_result, GMPG(rand_state), bits);
1824+
mpz_urandomb(gmpnum_result, GMPG(rand_state), (mp_bitcnt_t)bits);
18191825
}
18201826
/* }}} */
18211827

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)