mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
Fix pre-PHP 8.2 compatibility for php_mt_rand_range() with MT_RAND_PHP (#9839)
* Fix pre-PHP 8.2 compatibility for php_mt_rand_range() with MT_RAND_PHP As some left-over comments indicated: > Legacy mode deliberately not inside php_mt_rand_range() > to prevent other functions being affected The broken scaler was only used for `php_mt_rand_common()`, not `php_mt_rand_range()`. The former is only used for `mt_rand()`, whereas the latter is used for `array_rand()` and others. With the refactoring for the introduction of ext/random `php_mt_rand_common()` and `php_mt_rand_range()` were accidentally unified, thus introducing a behavioral change that was reported in FakerPHP/Faker#528. This commit moves the checks for `MT_RAND_PHP` from the general-purpose `range()` function back into `php_mt_rand_common()` and also into `Randomizer::getInt()` for drop-in compatibility with `mt_rand()`. * [ci skip] NEWS for `MT_RAND_PHP` compatibility
This commit is contained in:
parent
0db2e666a5
commit
7f0b228f48
5 changed files with 68 additions and 19 deletions
|
@ -126,7 +126,22 @@ PHP_METHOD(Random_Randomizer, getInt)
|
|||
RETURN_THROWS();
|
||||
}
|
||||
|
||||
result = randomizer->algo->range(randomizer->status, min, max);
|
||||
if (UNEXPECTED(
|
||||
randomizer->algo->range == php_random_algo_mt19937.range
|
||||
&& ((php_random_status_state_mt19937 *) randomizer->status->state)->mode != MT_RAND_MT19937
|
||||
)) {
|
||||
uint64_t r = php_random_algo_mt19937.generate(randomizer->status) >> 1;
|
||||
|
||||
/* This is an inlined version of the RAND_RANGE_BADSCALING macro that does not invoke UB when encountering
|
||||
* (max - min) > ZEND_LONG_MAX.
|
||||
*/
|
||||
zend_ulong offset = (double) ( (double) max - min + 1.0) * (r / (PHP_MT_RAND_MAX + 1.0));
|
||||
|
||||
result = (zend_long) (offset + min);
|
||||
} else {
|
||||
result = randomizer->algo->range(randomizer->status, min, max);
|
||||
}
|
||||
|
||||
if (EG(exception)) {
|
||||
RETURN_THROWS();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue