Skip to content

Fix rand_range32() for umax = UINT32_MAX #9416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ PHP NEWS
. Fixed bug GH-9285 (Traits cannot be used in readonly classes).
(kocsismate)

- Random:
. Fixed bug GH-9415 (Randomizer::getInt(0, 2**32 - 1) with Mt19937
always returns 1). (timwolla)

- Streams:
. Fixed bug GH-9316 ($http_response_header is wrong for long status line).
(cmb, timwolla)
Expand Down
2 changes: 1 addition & 1 deletion ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat

/* Special case where no modulus is required */
if (UNEXPECTED(umax == UINT32_MAX)) {
return true;
return result;
}

/* Increment the max so range is inclusive of max */
Expand Down
19 changes: 19 additions & 0 deletions ext/random/tests/03_randomizer/gh9415.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-9415: Randomizer::getInt(0, 2**32 - 1) with Mt19937 always returns 1
--FILE--
<?php

use Random\Randomizer;
use Random\Engine\Mt19937;

$randomizer = new Randomizer(new Mt19937(1234));
// Parameters shifted by -2147483648 to be compatible with 32-bit.
var_dump($randomizer->getInt(-2147483648, 2147483647));

$randomizer = new Randomizer(new Mt19937(4321));
var_dump($randomizer->getInt(-2147483648, 2147483647));

?>
--EXPECT--
int(-1324913873)
int(-1843387587)