Skip to content

Commit e059498

Browse files
authored
random: Fix unknown mt_srand() compatibility for unknown modes (#13544)
PHP 8.1 and below interpreted unknown modes as `MT_RAND_MT19937`, but PHP 8.2+ interprets them as `MT_RAND_PHP`. Align the behavior with PHP 8.1 and below, because folks should be steered towards the standard mode.
1 parent 99688db commit e059498

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ PHP NEWS
4545
. Fixed bug GH-13354 (pg_execute/pg_send_query_params/pg_send_execute
4646
with null value passed by reference). (George Barbarosie)
4747

48+
- Random:
49+
. Fixed bug GH-13544 (Pre-PHP 8.2 compatibility for mt_srand with
50+
unknown modes). (timwolla)
51+
4852
- Standard:
4953
. Fixed array key as hash to string (case insensitive) comparison typo
5054
for the second operand buffer size (albeit unused for now). (A. Slepykh)

ext/random/random.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,13 @@ PHP_FUNCTION(mt_srand)
688688
Z_PARAM_LONG(mode)
689689
ZEND_PARSE_PARAMETERS_END();
690690

691-
state->mode = mode;
691+
switch (mode) {
692+
case MT_RAND_PHP:
693+
state->mode = MT_RAND_PHP;
694+
break;
695+
default:
696+
state->mode = MT_RAND_MT19937;
697+
}
692698

693699
if (ZEND_NUM_ARGS() == 0) {
694700
php_random_mt19937_seed_default(status->state);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
mt_srand(): Test unknown modes
3+
--FILE--
4+
<?php
5+
// MT_RAND_MT19937
6+
mt_srand(1, 0);
7+
var_dump(mt_rand());
8+
// MT_RAND_PHP
9+
mt_srand(1, 1);
10+
var_dump(mt_rand());
11+
// Unknown
12+
mt_srand(1, 2);
13+
var_dump(mt_rand());
14+
// Equivalent to 0 when cast as unsigned 8-bit integer
15+
mt_srand(1, 256);
16+
var_dump(mt_rand());
17+
?>
18+
--EXPECT--
19+
int(895547922)
20+
int(1244335972)
21+
int(895547922)
22+
int(895547922)

0 commit comments

Comments
 (0)