Skip to content

Commit 0f696e2

Browse files
authored
Fix rand_range32() for umax = UINT32_MAX (php#9416)
* Fix rand_range32() for umax = UINT32_MAX This was introduced in the commit that added the random extension: 4d8dd8d. Resolves phpGH-9415 * [ci skip] Rename `$r` to `$randomizer` in gh9415.phpt * Make gh9415.phpt deterministic * Make gh9415.phpt compatible with 32-bit
1 parent b78c087 commit 0f696e2

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ PHP NEWS
1010
. Fixed bug GH-9285 (Traits cannot be used in readonly classes).
1111
(kocsismate)
1212

13+
- Random:
14+
. Fixed bug GH-9415 (Randomizer::getInt(0, 2**32 - 1) with Mt19937
15+
always returns 1). (timwolla)
16+
1317
- Streams:
1418
. Fixed bug GH-9316 ($http_response_header is wrong for long status line).
1519
(cmb, timwolla)

ext/random/random.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
107107

108108
/* Special case where no modulus is required */
109109
if (UNEXPECTED(umax == UINT32_MAX)) {
110-
return true;
110+
return result;
111111
}
112112

113113
/* Increment the max so range is inclusive of max */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-9415: Randomizer::getInt(0, 2**32 - 1) with Mt19937 always returns 1
3+
--FILE--
4+
<?php
5+
6+
use Random\Randomizer;
7+
use Random\Engine\Mt19937;
8+
9+
$randomizer = new Randomizer(new Mt19937(1234));
10+
// Parameters shifted by -2147483648 to be compatible with 32-bit.
11+
var_dump($randomizer->getInt(-2147483648, 2147483647));
12+
13+
$randomizer = new Randomizer(new Mt19937(4321));
14+
var_dump($randomizer->getInt(-2147483648, 2147483647));
15+
16+
?>
17+
--EXPECT--
18+
int(-1324913873)
19+
int(-1843387587)

0 commit comments

Comments
 (0)