From 68a7d62103a8c629c6239bff4f21fea832854d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 24 Aug 2022 12:20:18 +0200 Subject: [PATCH 1/4] Fix rand_range32() for umax = UINT32_MAX This was introduced in the commit that added the random extension: 4d8dd8d258ff365b146bcadcb277ede8992706d0. Resolves GH-9415 --- NEWS | 4 ++++ ext/random/random.c | 2 +- ext/random/tests/03_randomizer/gh9415.phpt | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 ext/random/tests/03_randomizer/gh9415.phpt diff --git a/NEWS b/NEWS index 67d13a61d5c2..df3ab007f7df 100644 --- a/NEWS +++ b/NEWS @@ -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) diff --git a/ext/random/random.c b/ext/random/random.c index ba25e5c04f04..b0cf8cb6f2c1 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -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 */ diff --git a/ext/random/tests/03_randomizer/gh9415.phpt b/ext/random/tests/03_randomizer/gh9415.phpt new file mode 100644 index 000000000000..ae8ca6734d78 --- /dev/null +++ b/ext/random/tests/03_randomizer/gh9415.phpt @@ -0,0 +1,18 @@ +--TEST-- +GH-9415: Randomizer::getInt(0, 2**32 - 1) with Mt19937 always returns 1 +--FILE-- +getInt(0, 2**32 - 1)); + +$r = new Randomizer(new Mt19937()); +var_dump($r->getInt(0, 2**32 - 1) !== 1 || $r->getInt(0, 2**32 - 1) !== $r->getInt(0, 2**32 - 1)); + +?> +--EXPECT-- +int(822569775) +bool(true) From d20c8cb45f6fbc54fb564f0b2a50b74c039be5d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 24 Aug 2022 12:31:36 +0200 Subject: [PATCH 2/4] [ci skip] Rename `$r` to `$randomizer` in gh9415.phpt --- ext/random/tests/03_randomizer/gh9415.phpt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/random/tests/03_randomizer/gh9415.phpt b/ext/random/tests/03_randomizer/gh9415.phpt index ae8ca6734d78..bfe55e3ef5a3 100644 --- a/ext/random/tests/03_randomizer/gh9415.phpt +++ b/ext/random/tests/03_randomizer/gh9415.phpt @@ -6,11 +6,11 @@ GH-9415: Randomizer::getInt(0, 2**32 - 1) with Mt19937 always returns 1 use Random\Randomizer; use Random\Engine\Mt19937; -$r = new Randomizer(new Mt19937(1234)); -var_dump($r->getInt(0, 2**32 - 1)); +$randomizer = new Randomizer(new Mt19937(1234)); +var_dump($randomizer->getInt(0, 2**32 - 1)); -$r = new Randomizer(new Mt19937()); -var_dump($r->getInt(0, 2**32 - 1) !== 1 || $r->getInt(0, 2**32 - 1) !== $r->getInt(0, 2**32 - 1)); +$randomizer = new Randomizer(new Mt19937()); +var_dump($randomizer->getInt(0, 2**32 - 1) !== 1 || $randomizer->getInt(0, 2**32 - 1) !== $randomizer->getInt(0, 2**32 - 1)); ?> --EXPECT-- From d1822b2bb05af139cccfec6face5ce9077448933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 24 Aug 2022 12:54:18 +0200 Subject: [PATCH 3/4] Make gh9415.phpt deterministic --- ext/random/tests/03_randomizer/gh9415.phpt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/random/tests/03_randomizer/gh9415.phpt b/ext/random/tests/03_randomizer/gh9415.phpt index bfe55e3ef5a3..3af34f29abb1 100644 --- a/ext/random/tests/03_randomizer/gh9415.phpt +++ b/ext/random/tests/03_randomizer/gh9415.phpt @@ -9,10 +9,10 @@ use Random\Engine\Mt19937; $randomizer = new Randomizer(new Mt19937(1234)); var_dump($randomizer->getInt(0, 2**32 - 1)); -$randomizer = new Randomizer(new Mt19937()); -var_dump($randomizer->getInt(0, 2**32 - 1) !== 1 || $randomizer->getInt(0, 2**32 - 1) !== $randomizer->getInt(0, 2**32 - 1)); +$randomizer = new Randomizer(new Mt19937(4321)); +var_dump($randomizer->getInt(0, 2**32 - 1)); ?> --EXPECT-- int(822569775) -bool(true) +int(304096061) From c9a12dd243d002a2f68e7c6188c797ba63be9e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 24 Aug 2022 13:38:51 +0200 Subject: [PATCH 4/4] Make gh9415.phpt compatible with 32-bit --- ext/random/tests/03_randomizer/gh9415.phpt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ext/random/tests/03_randomizer/gh9415.phpt b/ext/random/tests/03_randomizer/gh9415.phpt index 3af34f29abb1..c624cb785739 100644 --- a/ext/random/tests/03_randomizer/gh9415.phpt +++ b/ext/random/tests/03_randomizer/gh9415.phpt @@ -7,12 +7,13 @@ use Random\Randomizer; use Random\Engine\Mt19937; $randomizer = new Randomizer(new Mt19937(1234)); -var_dump($randomizer->getInt(0, 2**32 - 1)); +// 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(0, 2**32 - 1)); +var_dump($randomizer->getInt(-2147483648, 2147483647)); ?> --EXPECT-- -int(822569775) -int(304096061) +int(-1324913873) +int(-1843387587)