]> granicus.if.org Git - php/commitdiff
Promote mt_rand() min/max warning to ValueError
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 30 Oct 2019 09:36:42 +0000 (10:36 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 30 Oct 2019 09:36:42 +0000 (10:36 +0100)
ext/standard/basic_functions.stub.php
ext/standard/basic_functions_arginfo.h
ext/standard/mt_rand.c
ext/standard/tests/general_functions/bug46587.phpt

index 8322a63fc032e7afe853640e88a49976ba825105..8e0a2cde6b0c0dda13debc1fff383c7d332b87ee 100755 (executable)
@@ -953,8 +953,7 @@ function quoted_printable_encode(string $str): string {}
 
 function mt_srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {}
 
-/** @return int|false */
-function mt_rand(int $min = 0, int $max = PHP_INT_MAX) {}
+function mt_rand(int $min = 0, int $max = PHP_INT_MAX): int {}
 
 function mt_getrandmax(): int {}
 
index 6f16c42aea51e835fd5a7d09a0ceabe1afeff653..24735d679ff3f34b84776305db9493ae629f0b55 100755 (executable)
@@ -1241,7 +1241,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mt_srand, 0, 0, IS_VOID, 0)
        ZEND_ARG_TYPE_INFO(0, mode, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_rand, 0, 0, 0)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mt_rand, 0, 0, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, min, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, max, IS_LONG, 0)
 ZEND_END_ARG_INFO()
index d38b9cba8d28ea984b88936d5544fa8ad50e6548..9dff7da24c35057ddf4b875843671dd906dccf97 100644 (file)
@@ -325,8 +325,8 @@ PHP_FUNCTION(mt_rand)
        ZEND_PARSE_PARAMETERS_END();
 
        if (UNEXPECTED(max < min)) {
-               php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
-               RETURN_FALSE;
+               zend_value_error("max (" ZEND_LONG_FMT ") is smaller than min (" ZEND_LONG_FMT ")", max, min);
+               return;
        }
 
        RETURN_LONG(php_mt_rand_common(min, max));
index becbde964875597af7928ec0340f02c45d6c812e..ee59feb4445ea9dc1803fe0f61c52ea1abc46f5e 100644 (file)
@@ -4,13 +4,15 @@ Bug #46587 (mt_rand() does not check that max is greater than min).
 <?php
 
 var_dump(mt_rand(3,8));
-var_dump(mt_rand(8,3));
+try {
+    var_dump(mt_rand(8,3));
+} catch (ValueError $e) {
+    echo $e->getMessage(), "\n";
+}
 
 echo "Done.\n";
 ?>
 --EXPECTF--
 int(%d)
-
-Warning: mt_rand(): max(3) is smaller than min(8) in %s on line %d
-bool(false)
+max (3) is smaller than min (8)
 Done.