php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #64450
Patch bug64450.patch revision 2013-03-21 20:00 UTC by ab@php.net
revision 2013-03-21 14:03 UTC by ab@php.net

Patch bug64450.patch for *General Issues Bug #64450

Patch version 2013-03-21 20:00 UTC

Return to Bug #64450 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions: 2013-03-21 20:00 UTC | 2013-03-21 14:03 UTC

Developer: ab@php.net



  diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h
 index e831f32..bdeaed1 100644
 index e831f32..cc54702 100644
  --- a/ext/standard/php_rand.h
  +++ b/ext/standard/php_rand.h
  @@ -43,6 +43,10 @@
   #define RAND_RANGE(__n, __min, __max, __tmax) \
       (__n) = (__min) + (long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
   
  +#define RAND_RANGE_DOUBLE(__n, __min, __max, __tmax) \
 +    (__n) = (__min) + ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))
 +    (__n) = (__min) + ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))
  +
  +
   /* MT Rand */
   #define PHP_MT_RAND_MAX ((long) (0x7FFFFFFF)) /* (1<<31) - 1 */ 
   
  diff --git a/ext/standard/rand.c b/ext/standard/rand.c
 index 5f55a41..f716bc6 100644
 index 5f55a41..36938c0 100644
  --- a/ext/standard/rand.c
  +++ b/ext/standard/rand.c
  @@ -310,16 +310,15 @@ PHP_FUNCTION(rand)
      Returns a random number from Mersenne Twister */


  -		RAND_RANGE(number, min, max, PHP_MT_RAND_MAX);
  +		/* process the potentially overflowing variant*/
  +		number_one = (double) (php_mt_rand(TSRMLS_C) >> 1);
  +		RAND_RANGE_DOUBLE(number_one, min, max, PHP_MT_RAND_MAX);
 +		if (number_one <= LONG_MAX) {
 +		if (number_one >= 0 && number_one <= LONG_MAX || number_one < 0 && number_one >= (-LONG_MAX-1)) {
  +			RETURN_LONG((long)floor(number_one));
  +		} else {
  +			RETURN_DOUBLE(floor(number_one));
  +		}


  -	RETURN_LONG(number);
   }
   /* }}} */
   
 --- /dev/null	Thu Mar 21 14:20:31 2013
 --- /dev/null	Thu Mar 21 20:59:11 2013
  +++ ext/standard/tests/general_functions/bug64450.phpt	Thu Mar 21 14:19:45 2013
  @@ -0,0 +1,61 @@
  +--TEST--
  +Bug #64450 (mt_rand causes overflow within certain max value).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 19:01:30 2024 UTC