Skip to content

Fix UNKNOWN default parameters of rand() and mt_rand() #6184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,9 @@ function mt_srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {}
/** @alias mt_srand */
function srand(int $seed = 0, int $mode = MT_RAND_MT19937): void {}

function rand(int $min = UNKNOWN, int $max = UNKNOWN): int {}
function rand(int $min = 0, int $max = PHP_INT_MAX): int {}

function mt_rand(int $min = UNKNOWN, int $max = UNKNOWN): int {}
function mt_rand(int $min = 0, int $max = PHP_INT_MAX): int {}

function mt_getrandmax(): int {}

Expand Down
6 changes: 3 additions & 3 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 3c02183529eed2eb21d801ed2ba615deaf749b1d */
* Stub hash: 131f77fd51f0d9b5e0354c6e30da6abd29d4fa47 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
Expand Down Expand Up @@ -1823,8 +1823,8 @@ ZEND_END_ARG_INFO()
#define arginfo_srand arginfo_mt_srand

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_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_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, min, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, max, IS_LONG, 0, "PHP_INT_MAX")
ZEND_END_ARG_INFO()

#define arginfo_mt_rand arginfo_rand
Expand Down
7 changes: 4 additions & 3 deletions ext/standard/mt_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,17 @@ PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
/* {{{ Returns a random number from Mersenne Twister */
PHP_FUNCTION(mt_rand)
{
zend_long min;
zend_long max;
zend_long min = 0;
zend_long max = ZEND_LONG_MAX;
int argc = ZEND_NUM_ARGS();

if (argc == 0) {
// genrand_int31 in mt19937ar.c performs a right shift
RETURN_LONG(php_mt_rand() >> 1);
}
Comment on lines 310 to 313
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These blocks should come after ZPP and you need to make the function work with one argument now for this to work.


ZEND_PARSE_PARAMETERS_START(2, 2)
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(min)
Z_PARAM_LONG(max)
ZEND_PARSE_PARAMETERS_END();
Expand Down
7 changes: 4 additions & 3 deletions ext/standard/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ PHPAPI zend_long php_rand(void)
/* {{{ Returns a random number from Mersenne Twister */
PHP_FUNCTION(rand)
{
zend_long min;
zend_long max;
zend_long min = 0;
zend_long max = ZEND_LONG_MAX;
int argc = ZEND_NUM_ARGS();

if (argc == 0) {
RETURN_LONG(php_mt_rand() >> 1);
}

ZEND_PARSE_PARAMETERS_START(2, 2)
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(min)
Z_PARAM_LONG(max)
ZEND_PARSE_PARAMETERS_END();
Expand Down