File tree Expand file tree Collapse file tree 4 files changed +28
-6
lines changed Expand file tree Collapse file tree 4 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ PHP NEWS
45
45
. Fixed floating point exception bug with gmp_pow when using
46
46
large exposant values. (David Carlier).
47
47
. Fixed bug GH-16411 (gmp_export() can cause overflow). (cmb)
48
+ . Fixed bug GH-16501 (gmp_random_bits() can cause overflow).
49
+ (David Carlier)
48
50
49
51
- MBstring:
50
52
. Fixed bug GH-16361 (mb_substr overflow on start/length arguments).
Original file line number Diff line number Diff line change @@ -1803,15 +1803,21 @@ ZEND_FUNCTION(gmp_random_bits)
1803
1803
RETURN_THROWS ();
1804
1804
}
1805
1805
1806
- if (bits <= 0 ) {
1807
- zend_argument_value_error (1 , "must be greater than or equal to 1" );
1806
+ #if SIZEOF_SIZE_T == 4
1807
+ const zend_long maxbits = ULONG_MAX / GMP_NUMB_BITS ;
1808
+ #else
1809
+ const zend_long maxbits = INT_MAX ;
1810
+ #endif
1811
+
1812
+ if (bits <= 0 || bits > maxbits ) {
1813
+ zend_argument_value_error (1 , "must be between 1 and " ZEND_LONG_FMT , maxbits );
1808
1814
RETURN_THROWS ();
1809
1815
}
1810
1816
1811
1817
INIT_GMP_RETVAL (gmpnum_result );
1812
1818
gmp_init_random ();
1813
1819
1814
- mpz_urandomb (gmpnum_result , GMPG (rand_state ), bits );
1820
+ mpz_urandomb (gmpnum_result , GMPG (rand_state ), ( mp_bitcnt_t ) bits );
1815
1821
}
1816
1822
/* }}} */
1817
1823
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ while (1) {
40
40
41
41
echo "Done \n" ;
42
42
?>
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
46
46
Done
You can’t perform that action at this time.
0 commit comments