From 1a74f1f4c0a96835e40ae63852deb5f265d276ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Thu, 19 Jan 2023 20:15:42 +0100 Subject: [PATCH 1/4] Fix GH-10292 make the default value of mt_srand() nullable --- ext/random/random.c | 5 +++-- ext/random/random.stub.php | 4 ++-- ext/random/random_arginfo.h | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index cdc98fbf242d7..6a6afd7b5dc14 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -675,19 +675,20 @@ PHP_FUNCTION(lcg_value) PHP_FUNCTION(mt_srand) { zend_long seed = 0; + bool seed_is_null = 1; zend_long mode = MT_RAND_MT19937; php_random_status *status = RANDOM_G(mt19937); php_random_status_state_mt19937 *state = status->state; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL - Z_PARAM_LONG(seed) + Z_PARAM_LONG_OR_NULL(seed, seed_is_null) Z_PARAM_LONG(mode) ZEND_PARSE_PARAMETERS_END(); state->mode = mode; - if (ZEND_NUM_ARGS() == 0) { + if (seed_is_null) { php_random_mt19937_seed_default(status->state); } else { php_random_algo_mt19937.seed(status, (uint64_t) seed); diff --git a/ext/random/random.stub.php b/ext/random/random.stub.php index e87aec3a27ced..d03057d7a6a6e 100644 --- a/ext/random/random.stub.php +++ b/ext/random/random.stub.php @@ -16,10 +16,10 @@ function lcg_value(): float {} - function mt_srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {} + function mt_srand(?int $seed = null, int $mode = MT_RAND_MT19937): void {} /** @alias mt_srand */ - function srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {} + function srand(?int $seed = null, int $mode = MT_RAND_MT19937): void {} function rand(int $min = UNKNOWN, int $max = UNKNOWN): int {} diff --git a/ext/random/random_arginfo.h b/ext/random/random_arginfo.h index 5fc34c95aee63..967dd2d2eb290 100644 --- a/ext/random/random_arginfo.h +++ b/ext/random/random_arginfo.h @@ -1,11 +1,11 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 7b9594d2eadb778ecec34114b67f2d0ae8bbb58a */ + * Stub hash: 533dc78162cc1e510f7c87971a6350acd43de1ab */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_lcg_value, 0, 0, IS_DOUBLE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mt_srand, 0, 0, IS_VOID, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, seed, IS_LONG, 0, "0") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, seed, IS_LONG, 1, "null") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, mode, IS_LONG, 0, "MT_RAND_MT19937") ZEND_END_ARG_INFO() From 3da0bf501916b0b881f477b93954abc3d9cae20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Thu, 19 Jan 2023 21:50:37 +0100 Subject: [PATCH 2/4] Update ext/random/random.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Düsterhus --- ext/random/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/random/random.c b/ext/random/random.c index 6a6afd7b5dc14..30ae6c3baa7bc 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -675,7 +675,7 @@ PHP_FUNCTION(lcg_value) PHP_FUNCTION(mt_srand) { zend_long seed = 0; - bool seed_is_null = 1; + bool seed_is_null = true; zend_long mode = MT_RAND_MT19937; php_random_status *status = RANDOM_G(mt19937); php_random_status_state_mt19937 *state = status->state; From cd92bcb541f3cc7c06a8f34870687174a6f7ea1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Fri, 20 Jan 2023 21:04:37 +0100 Subject: [PATCH 3/4] [skip ci] Add upgrading note --- UPGRADING | 2 ++ 1 file changed, 2 insertions(+) diff --git a/UPGRADING b/UPGRADING index 9fdfdb88f9280..8ae21aa9ddf68 100644 --- a/UPGRADING +++ b/UPGRADING @@ -85,6 +85,8 @@ PHP 8.3 UPGRADE NOTES RFC: https://wiki.php.net/rfc/randomizer_additions . Added Randomizer::nextFloat(), ::getFloat(), and IntervalBoundary. RFC: https://wiki.php.net/rfc/randomizer_additions + . Changed the default value of the first parameter ($seed) of mt_srand() and + srand() to be null rather than 0. - Sockets: . Added socket_atmark to checks if the socket is OOB marked. From 3bff19d1f0f0ab6c9191e80731e33aea3604163e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Fri, 20 Jan 2023 23:32:55 +0100 Subject: [PATCH 4/4] Improve wording --- NEWS | 2 ++ UPGRADING | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index ee4dcfc207df6..203ba7f3b108a 100644 --- a/NEWS +++ b/NEWS @@ -76,6 +76,8 @@ PHP NEWS - Random: . Added Randomizer::getBytesFromString(). (Joshua Rüsweg) . Added Randomizer::nextFloat(), ::getFloat(), and IntervalBoundary. (timwolla) + . Fix GH-10292 (Made the default value of the first param of srand() and + mt_srand() nullable). (kocsismate) - Reflection: . Fix GH-9470 (ReflectionMethod constructor should not find private parent diff --git a/UPGRADING b/UPGRADING index 8ae21aa9ddf68..7e1e5bf8f56b6 100644 --- a/UPGRADING +++ b/UPGRADING @@ -85,8 +85,10 @@ PHP 8.3 UPGRADE NOTES RFC: https://wiki.php.net/rfc/randomizer_additions . Added Randomizer::nextFloat(), ::getFloat(), and IntervalBoundary. RFC: https://wiki.php.net/rfc/randomizer_additions - . Changed the default value of the first parameter ($seed) of mt_srand() and - srand() to be null rather than 0. + . Changed mt_srand() and srand() to not check the number of arguments to + determine whether a random seed should be used. Passing null will generate + a random seed, 0 will use zero as the seed. The functions are now consistent + with Mt19937::__construct(). - Sockets: . Added socket_atmark to checks if the socket is OOB marked.