From e059498c043c7325d38d2aa2caa8a5a5dfde4669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 29 Feb 2024 18:05:59 +0100 Subject: [PATCH] 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. --- NEWS | 4 ++++ ext/random/random.c | 8 ++++++- .../01_functions/mt_srand_unknown_mode.phpt | 22 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 ext/random/tests/01_functions/mt_srand_unknown_mode.phpt diff --git a/NEWS b/NEWS index fa1a89dd1b063..18ebec1081fdd 100644 --- a/NEWS +++ b/NEWS @@ -45,6 +45,10 @@ PHP NEWS . Fixed bug GH-13354 (pg_execute/pg_send_query_params/pg_send_execute with null value passed by reference). (George Barbarosie) +- Random: + . Fixed bug GH-13544 (Pre-PHP 8.2 compatibility for mt_srand with + unknown modes). (timwolla) + - Standard: . Fixed array key as hash to string (case insensitive) comparison typo for the second operand buffer size (albeit unused for now). (A. Slepykh) diff --git a/ext/random/random.c b/ext/random/random.c index dda57f0fe7d35..9f9da28962d15 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -688,7 +688,13 @@ PHP_FUNCTION(mt_srand) Z_PARAM_LONG(mode) ZEND_PARSE_PARAMETERS_END(); - state->mode = mode; + switch (mode) { + case MT_RAND_PHP: + state->mode = MT_RAND_PHP; + break; + default: + state->mode = MT_RAND_MT19937; + } if (ZEND_NUM_ARGS() == 0) { php_random_mt19937_seed_default(status->state); diff --git a/ext/random/tests/01_functions/mt_srand_unknown_mode.phpt b/ext/random/tests/01_functions/mt_srand_unknown_mode.phpt new file mode 100644 index 0000000000000..f4a3cc181ccc8 --- /dev/null +++ b/ext/random/tests/01_functions/mt_srand_unknown_mode.phpt @@ -0,0 +1,22 @@ +--TEST-- +mt_srand(): Test unknown modes +--FILE-- + +--EXPECT-- +int(895547922) +int(1244335972) +int(895547922) +int(895547922)