Skip to content

Commit 1f05d6e

Browse files
kocsismateTimWolla
andauthored
Fix GH-10292 make the default value of the first parame of srand() and mt_srand() nullable (#10380)
Co-authored-by: Tim Düsterhus <[email protected]>
1 parent db6840b commit 1f05d6e

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ PHP NEWS
7676
- Random:
7777
. Added Randomizer::getBytesFromString(). (Joshua Rüsweg)
7878
. Added Randomizer::nextFloat(), ::getFloat(), and IntervalBoundary. (timwolla)
79+
. Fix GH-10292 (Made the default value of the first param of srand() and
80+
mt_srand() nullable). (kocsismate)
7981

8082
- Reflection:
8183
. Fix GH-9470 (ReflectionMethod constructor should not find private parent

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ PHP 8.3 UPGRADE NOTES
8888
RFC: https://wiki.php.net/rfc/randomizer_additions
8989
. Added Randomizer::nextFloat(), ::getFloat(), and IntervalBoundary.
9090
RFC: https://wiki.php.net/rfc/randomizer_additions
91+
. Changed mt_srand() and srand() to not check the number of arguments to
92+
determine whether a random seed should be used. Passing null will generate
93+
a random seed, 0 will use zero as the seed. The functions are now consistent
94+
with Mt19937::__construct().
9195

9296
- Sockets:
9397
. Added socket_atmark to checks if the socket is OOB marked.

ext/random/random.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,19 +675,20 @@ PHP_FUNCTION(lcg_value)
675675
PHP_FUNCTION(mt_srand)
676676
{
677677
zend_long seed = 0;
678+
bool seed_is_null = true;
678679
zend_long mode = MT_RAND_MT19937;
679680
php_random_status *status = RANDOM_G(mt19937);
680681
php_random_status_state_mt19937 *state = status->state;
681682

682683
ZEND_PARSE_PARAMETERS_START(0, 2)
683684
Z_PARAM_OPTIONAL
684-
Z_PARAM_LONG(seed)
685+
Z_PARAM_LONG_OR_NULL(seed, seed_is_null)
685686
Z_PARAM_LONG(mode)
686687
ZEND_PARSE_PARAMETERS_END();
687688

688689
state->mode = mode;
689690

690-
if (ZEND_NUM_ARGS() == 0) {
691+
if (seed_is_null) {
691692
php_random_mt19937_seed_default(status->state);
692693
} else {
693694
php_random_algo_mt19937.seed(status, (uint64_t) seed);

ext/random/random.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
function lcg_value(): float {}
1818

19-
function mt_srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {}
19+
function mt_srand(?int $seed = null, int $mode = MT_RAND_MT19937): void {}
2020

2121
/** @alias mt_srand */
22-
function srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {}
22+
function srand(?int $seed = null, int $mode = MT_RAND_MT19937): void {}
2323

2424
function rand(int $min = UNKNOWN, int $max = UNKNOWN): int {}
2525

ext/random/random_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)