]> granicus.if.org Git - php/commitdiff
Preserve BC for rand() AND mt_rand() where min > max
authorLeigh <leigh@php.net>
Wed, 10 Aug 2016 22:32:32 +0000 (23:32 +0100)
committerLeigh <leigh@php.net>
Wed, 10 Aug 2016 22:32:32 +0000 (23:32 +0100)
ext/standard/basic_functions.c
ext/standard/mt_rand.c
ext/standard/php_math.h
ext/standard/php_mt_rand.h
ext/standard/rand.c

index ef03db14db36d4cdb3c1e4cee38b6d946f8c0207..88e628ae05cd7ab27bdc7c49dcf396020e10bc08 100644 (file)
@@ -2851,7 +2851,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FE(proc_nice,                                                                                                               arginfo_proc_nice)
 #endif
 
-       PHP_FALIAS(rand, mt_rand,                                                                                               arginfo_mt_rand)
+       PHP_FE(rand,                                                                                                                    arginfo_mt_rand)
        PHP_FALIAS(srand, mt_srand,                                                                                             arginfo_mt_srand)
        PHP_FALIAS(getrandmax, mt_getrandmax,                                                                   arginfo_mt_getrandmax)
        PHP_FE(mt_rand,                                                                                                                 arginfo_mt_rand)
index f2866c5af2f19b8e9e082a9043b7c72b6d803f58..dde9a771349d9231d155fe27fda81936a9931ba0 100644 (file)
@@ -256,13 +256,31 @@ PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max)
 }
 /* }}} */
 
+/* {{{ php_mt_rand_common
+ * rand() allows min > max, mt_rand does not */
+PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
+{
+       zend_long n;
+
+       if (BG(mt_rand_mode) == MT_RAND_MT19937) {
+               return 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 n;
+}
+/* }}} */
+
 /* {{{ proto int mt_rand([int min, int max])
    Returns a random number from Mersenne Twister */
 PHP_FUNCTION(mt_rand)
 {
        zend_long min;
        zend_long max;
-       zend_long n;
        int argc = ZEND_NUM_ARGS();
 
        if (argc == 0) {
@@ -276,18 +294,10 @@ PHP_FUNCTION(mt_rand)
 
        if (UNEXPECTED(max < min)) {
                php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
-               max ^= min ^= max ^= min;
+               RETURN_FALSE;
        }
 
-       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);
+       RETURN_LONG(php_mt_rand_common(min, max));
 }
 /* }}} */
 
index 9798db84227d62b81cf2025938c595994efff890..bc5b5125ee3624e29b1a156d9d400ee7b8c8e999 100644 (file)
@@ -45,6 +45,7 @@ PHP_FUNCTION(is_infinite);
 PHP_FUNCTION(is_nan);
 PHP_FUNCTION(pow);
 PHP_FUNCTION(sqrt);
+PHP_FUNCTION(rand);
 PHP_FUNCTION(mt_srand);
 PHP_FUNCTION(mt_rand);
 PHP_FUNCTION(mt_getrandmax);
index bf0f6c20a12b0bfdc7f190d4e4eea08b1cc571e2..e33d5585919487331ba6add19ab64fb024d697e8 100644 (file)
@@ -36,6 +36,7 @@
 PHPAPI void php_mt_srand(uint32_t seed);
 PHPAPI uint32_t php_mt_rand(void);
 PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
+PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
 
 PHP_MINIT_FUNCTION(mt_rand);
 
index 2720f55661f7c1595aa80e4cf67714e056c187c1..e97e0b46f03ceb0f5869ca5613986475fe015fed 100644 (file)
@@ -45,6 +45,30 @@ PHPAPI zend_long php_rand(void)
 }
 /* }}} */
 
+/* {{{ proto int mt_rand([int min, int max])
+   Returns a random number from Mersenne Twister */
+PHP_FUNCTION(rand)
+{
+       zend_long min;
+       zend_long max;
+       int argc = ZEND_NUM_ARGS();
+
+       if (argc == 0) {
+               RETURN_LONG(php_mt_rand() >> 1);
+       }
+
+       if (zend_parse_parameters(argc, "ll", &min, &max) == FAILURE) {
+               return;
+       }
+
+       if (max < min) {
+               RETURN_LONG(php_mt_rand_common(max, min));
+       }
+
+       RETURN_LONG(php_mt_rand_common(min, max));
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4