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 9fdfdb88f9280..7e1e5bf8f56b6 100644 --- a/UPGRADING +++ b/UPGRADING @@ -85,6 +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 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. diff --git a/ext/random/random.c b/ext/random/random.c index cdc98fbf242d7..30ae6c3baa7bc 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 = true; 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()