Skip to content

Commit e704e1e

Browse files
committed
Fixed bug #75514 mt_rand returns value outside [$min,$max]+ on 32-bit
1 parent c718fd1 commit e704e1e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

ext/standard/mt_rand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max)
260260
* rand() allows min > max, mt_rand does not */
261261
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
262262
{
263-
zend_long n;
263+
uint32_t n;
264264

265265
if (BG(mt_rand_mode) == MT_RAND_MT19937) {
266266
return php_mt_rand_range(min, max);
267267
}
268268

269269
/* Legacy mode deliberately not inside php_mt_rand_range()
270270
* to prevent other functions being affected */
271-
n = (zend_long)php_mt_rand() >> 1;
271+
n = php_mt_rand() >> 1;
272272
RAND_RANGE_BADSCALING(n, min, max, PHP_MT_RAND_MAX);
273273

274274
return n;

ext/standard/tests/math/bug75514.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug #75514 mt_rand returns value outside [$min,$max]
3+
--FILE--
4+
<?php
5+
mt_srand(0, MT_RAND_PHP);
6+
var_dump(mt_rand(0,999999999), mt_rand(0,999));
7+
?>
8+
===Done===
9+
--EXPECT--
10+
int(448865905)
11+
int(592)
12+
===Done===

0 commit comments

Comments
 (0)