Skip to content

Commit 1628995

Browse files
committed
Standard: use if available php_random_bytes() in mt_rand() / mt_srand().
1 parent c5fed6d commit 1628995

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

ext/standard/mt_rand.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "php.h"
2626
#include "php_rand.h"
27+
#include "php_random.h"
2728
#include "php_mt_rand.h"
2829

2930
/* MT RAND FUNCTIONS */
@@ -161,7 +162,11 @@ PHPAPI uint32_t php_mt_rand(void)
161162
register uint32_t s1;
162163

163164
if (UNEXPECTED(!BG(mt_rand_is_seeded))) {
164-
php_mt_srand(GENERATE_SEED());
165+
zend_long bytes;
166+
if (php_random_bytes_silent(&bytes, sizeof(zend_long)) == FAILURE) {
167+
bytes = GENERATE_SEED();
168+
}
169+
php_mt_srand(bytes);
165170
}
166171

167172
if (BG(left) == 0) {
@@ -189,8 +194,11 @@ PHP_FUNCTION(mt_srand)
189194
Z_PARAM_LONG(mode)
190195
ZEND_PARSE_PARAMETERS_END();
191196

192-
if (ZEND_NUM_ARGS() == 0)
193-
seed = GENERATE_SEED();
197+
if (ZEND_NUM_ARGS() == 0) {
198+
if (php_random_bytes_silent(&seed, sizeof(zend_long)) == FAILURE) {
199+
seed = GENERATE_SEED();
200+
}
201+
}
194202

195203
switch (mode) {
196204
case MT_RAND_PHP:

0 commit comments

Comments
 (0)