Skip to content

Commit becda2e

Browse files
committed
Promote mt_rand() min/max warning to ValueError
1 parent ad9ea5a commit becda2e

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,7 @@ function quoted_printable_encode(string $str): string {}
953953

954954
function mt_srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {}
955955

956-
/** @return int|false */
957-
function mt_rand(int $min = 0, int $max = PHP_INT_MAX) {}
956+
function mt_rand(int $min = 0, int $max = PHP_INT_MAX): int {}
958957

959958
function mt_getrandmax(): int {}
960959

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mt_srand, 0, 0, IS_VOID, 0)
12411241
ZEND_ARG_TYPE_INFO(0, mode, IS_LONG, 0)
12421242
ZEND_END_ARG_INFO()
12431243

1244-
ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_rand, 0, 0, 0)
1244+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mt_rand, 0, 0, IS_LONG, 0)
12451245
ZEND_ARG_TYPE_INFO(0, min, IS_LONG, 0)
12461246
ZEND_ARG_TYPE_INFO(0, max, IS_LONG, 0)
12471247
ZEND_END_ARG_INFO()

ext/standard/mt_rand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ PHP_FUNCTION(mt_rand)
325325
ZEND_PARSE_PARAMETERS_END();
326326

327327
if (UNEXPECTED(max < min)) {
328-
php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
329-
RETURN_FALSE;
328+
zend_value_error("max (" ZEND_LONG_FMT ") is smaller than min (" ZEND_LONG_FMT ")", max, min);
329+
return;
330330
}
331331

332332
RETURN_LONG(php_mt_rand_common(min, max));

ext/standard/tests/general_functions/bug46587.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Bug #46587 (mt_rand() does not check that max is greater than min).
44
<?php
55

66
var_dump(mt_rand(3,8));
7-
var_dump(mt_rand(8,3));
7+
try {
8+
var_dump(mt_rand(8,3));
9+
} catch (ValueError $e) {
10+
echo $e->getMessage(), "\n";
11+
}
812

913
echo "Done.\n";
1014
?>
1115
--EXPECTF--
1216
int(%d)
13-
14-
Warning: mt_rand(): max(3) is smaller than min(8) in %s on line %d
15-
bool(false)
17+
max (3) is smaller than min (8)
1618
Done.

0 commit comments

Comments
 (0)