Last active
May 13, 2016 05:44
-
-
Save yohgaki/1519f65dffd66735bafe to your computer and use it in GitHub Desktop.
mt_rand/rand should raise error exceeds getrandmax() value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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