com php-src: Fix legacy mode RAND_RANGE and 32/64 -bit consistency: ext/standard/mt_rand.c ext/standa rd/php_rand.h

From: Date: Sat, 16 Jul 2016 16:50:20 +0000
Subject: com php-src: Fix legacy mode RAND_RANGE and 32/64 -bit consistency: ext/standard/mt_rand.c ext/standa rd/php_rand.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-93058@lists.php.net to get a copy of this message
Commit: 027375d4c3a59b0f174b1beb70ba8f60efef6905 Author: Leigh <leigh@php.net> Sat, 16 Jul 2016 16:50:20 +0000 Parents: be3640150cb2f88d38a8d405b2bf708f04be39ef Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=027375d4c3a59b0f174b1beb70ba8f60efef6905 Log: Fix legacy mode RAND_RANGE and 32/64-bit consistency Changed paths: M ext/standard/mt_rand.c M ext/standard/php_rand.h Diff: diff --git a/ext/standard/mt_rand.c b/ext/standard/mt_rand.c index fa7c004..bf9ce66 100644 --- a/ext/standard/mt_rand.c +++ b/ext/standard/mt_rand.c @@ -217,11 +217,10 @@ PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max) zend_ulong limit; zend_ulong result; -#if ZEND_ULONG_MAX > UINT32_MAX - result = ((zend_ulong)php_mt_rand() << 32) | php_mt_rand(); -#else result = php_mt_rand(); -#endif + if (umax > UINT32_MAX) { + result = (result << 32) | php_mt_rand(); + } /* Special case where no modulus is required */ if (UNEXPECTED(umax == ZEND_ULONG_MAX)) { @@ -238,11 +237,7 @@ PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max) /* Discard numbers over the limit to avoid modulo bias */ while (UNEXPECTED(result > limit)) { -#if ZEND_ULONG_MAX > UINT32_MAX result = (result << 32) | php_mt_rand(); -#else - result = php_mt_rand(); -#endif } } @@ -256,6 +251,7 @@ PHP_FUNCTION(mt_rand) { zend_long min; zend_long max; + zend_long n; int argc = ZEND_NUM_ARGS(); if (argc == 0) { @@ -272,7 +268,15 @@ PHP_FUNCTION(mt_rand) RETURN_FALSE; } - RETURN_LONG(php_mt_rand_range(min, max)); + if (BG(mt_rand_mode) == MT_RAND_MT19937) { + RETURN_LONG(php_mt_rand_range(min, max)); + } + + /* Legacy mode deliberately not inside php_mt_rand_range() + * to prevent other functions being affected */ + n = (zend_long)php_mt_rand() >> 1; + RAND_RANGE_BADSCALING(n, min, max, PHP_MT_RAND_MAX); + RETURN_LONG(n); } /* }}} */ diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h index 76a3684..506e497 100644 --- a/ext/standard/php_rand.h +++ b/ext/standard/php_rand.h @@ -60,6 +60,9 @@ * * -RL */ +#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))

« previous php.cvs (#93058) next »