From: Tom Van Looy Date: Sun, 3 Dec 2017 21:35:50 +0000 (+0100) Subject: Remove RAND_RANGE() macro X-Git-Tag: php-7.3.0alpha1~839 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e4e26f24285511c5aa2a359ff7e93c1748fdda3b;p=php Remove RAND_RANGE() macro The behavior of RANGE_RANGE() is 7.1 changed completely, from rescaling an already generated number to generating a number itself. Because of this str_shuffle() ended up generating two random numbers on every iteration. To avoid further misuse the function is dropped entirely. Extensions for PHP >= 7.1 should directly call php_mt_rand_range(). --- diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 7dce0fb731..c5a41f2198 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -9,6 +9,7 @@ PHP 7.3 INTERNALS UPGRADE NOTES f. GC_REFCOUNT() g. zend_get_parameters() h. zend_register_persistent_resource() + i. RAND_RANGE() 2. Build system changes a. Unix build system changes @@ -82,6 +83,9 @@ PHP 7.3 INTERNALS UPGRADE NOTES zend_register_persistent_resource_ex() should beused to register persistent resources, instead of manual insertion into EG(persistent_list). + i. The RANGE_RANGE() macro has been removed. php_mt_rand_range() should be + used instead. + ======================== 2. Build system changes ======================== diff --git a/ext/standard/array.c b/ext/standard/array.c index dfeaef8747..05c89d901b 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -2968,7 +2968,7 @@ static void php_array_data_shuffle(zval *array) /* {{{ */ } } while (--n_left) { - RAND_RANGE(rnd_idx, 0, n_left, PHP_RAND_MAX); + rnd_idx = php_mt_rand_range(0, n_left); if (rnd_idx != n_left) { temp = hash->arData[n_left]; hash->arData[n_left] = hash->arData[rnd_idx]; @@ -2993,7 +2993,7 @@ static void php_array_data_shuffle(zval *array) /* {{{ */ } } while (--n_left) { - RAND_RANGE(rnd_idx, 0, n_left, PHP_RAND_MAX); + rnd_idx = php_mt_rand_range(0, n_left); if (rnd_idx != n_left) { temp = hash->arData[n_left]; hash->arData[n_left] = hash->arData[rnd_idx]; diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h index b4a6613bee..497a539e4e 100644 --- a/ext/standard/php_rand.h +++ b/ext/standard/php_rand.h @@ -63,9 +63,6 @@ #define RAND_RANGE_BADSCALING(__n, __min, __max, __tmax) \ (__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))) -#define RAND_RANGE(__n, __min, __max, __tmax) \ - (__n) = php_mt_rand_range((__min), (__max)) - #ifdef PHP_WIN32 #define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg()))) #else diff --git a/ext/standard/string.c b/ext/standard/string.c index 1ef96e9132..2526dae144 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -5479,8 +5479,7 @@ static void php_string_shuffle(char *str, zend_long len) /* {{{ */ n_left = n_elems; while (--n_left) { - rnd_idx = php_rand(); - RAND_RANGE(rnd_idx, 0, n_left, PHP_RAND_MAX); + rnd_idx = php_mt_rand_range(0, n_left); if (rnd_idx != n_left) { temp = str[n_left]; str[n_left] = str[rnd_idx];