Skip to content

Instantly share code, notes, and snippets.

@yohgaki
Last active May 13, 2016 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yohgaki/1519f65dffd66735bafe to your computer and use it in GitHub Desktop.
Save yohgaki/1519f65dffd66735bafe to your computer and use it in GitHub Desktop.
mt_rand/rand should raise error exceeds getrandmax() value
diff --git a/ext/standard/rand.c b/ext/standard/rand.c
index 50729f2..38c405d 100644
--- a/ext/standard/rand.c
+++ b/ext/standard/rand.c
@@ -293,8 +293,16 @@ PHP_FUNCTION(rand)
zend_long number;
int argc = ZEND_NUM_ARGS();
- if (argc != 0 && zend_parse_parameters(argc, "ll", &min, &max) == FAILURE)
- return;
+ if (argc != 0) {
+ if (zend_parse_parameters(argc, "ll", &min, &max) == FAILURE) {
+ return;
+ } else if (max < min) {
+ php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
+ RETURN_FALSE;
+ } else if (max - min > PHP_RAND_MAX) {
+ php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") - min(" ZEND_LONG_FMT ") range exceeds getrandmax() value", max, min);
+ }
+ }
number = php_rand();
if (argc == 2) {
@@ -320,6 +328,8 @@ PHP_FUNCTION(mt_rand)
} else if (max < min) {
php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
RETURN_FALSE;
+ } else if (max - min > PHP_MT_RAND_MAX) {
+ php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") - min(" ZEND_LONG_FMT ") range exceeds mt_getrandmax() value", max, min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment