Skip to content

Commit 45714e2

Browse files
jorgsowaTimWolla
andauthored
random: Remove redundant assignments in php_random_rangeX() (#14536)
Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
1 parent 61a0e3b commit 45714e2

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

ext/random/random.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t u
8181
const php_random_algo *algo = engine.algo;
8282
void *state = engine.state;
8383

84-
uint32_t result, limit;
85-
size_t total_size = 0;
86-
uint32_t count = 0;
84+
uint32_t result;
85+
size_t total_size;
8786

8887
result = 0;
8988
total_size = 0;
@@ -110,9 +109,10 @@ PHPAPI uint32_t php_random_range32(php_random_algo_with_state engine, uint32_t u
110109
}
111110

112111
/* Ceiling under which UINT32_MAX % max == 0 */
113-
limit = UINT32_MAX - (UINT32_MAX % umax) - 1;
112+
uint32_t limit = UINT32_MAX - (UINT32_MAX % umax) - 1;
114113

115114
/* Discard numbers over the limit to avoid modulo bias */
115+
uint32_t count = 0;
116116
while (UNEXPECTED(result > limit)) {
117117
/* If the requirements cannot be met in a cycles, return fail */
118118
if (++count > PHP_RANDOM_RANGE_ATTEMPTS) {
@@ -140,9 +140,8 @@ PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t u
140140
const php_random_algo *algo = engine.algo;
141141
void *state = engine.state;
142142

143-
uint64_t result, limit;
144-
size_t total_size = 0;
145-
uint32_t count = 0;
143+
uint64_t result;
144+
size_t total_size;
146145

147146
result = 0;
148147
total_size = 0;
@@ -169,9 +168,10 @@ PHPAPI uint64_t php_random_range64(php_random_algo_with_state engine, uint64_t u
169168
}
170169

171170
/* Ceiling under which UINT64_MAX % max == 0 */
172-
limit = UINT64_MAX - (UINT64_MAX % umax) - 1;
171+
uint64_t limit = UINT64_MAX - (UINT64_MAX % umax) - 1;
173172

174173
/* Discard numbers over the limit to avoid modulo bias */
174+
uint32_t count = 0;
175175
while (UNEXPECTED(result > limit)) {
176176
/* If the requirements cannot be met in a cycles, return fail */
177177
if (++count > PHP_RANDOM_RANGE_ATTEMPTS) {
@@ -520,7 +520,7 @@ PHP_FUNCTION(mt_getrandmax)
520520
ZEND_PARSE_PARAMETERS_NONE();
521521

522522
/*
523-
* Melo: it could be 2^^32 but we only use 2^^31 to maintain
523+
* Melo: it could be 2^^32, but we only use 2^^31 to maintain
524524
* compatibility with the previous php_rand
525525
*/
526526
RETURN_LONG(PHP_MT_RAND_MAX); /* 2^^31 */
@@ -614,7 +614,7 @@ PHPAPI uint64_t php_random_generate_fallback_seed(void)
614614
{
615615
/* Mix various values using SHA-1 as a PRF to obtain as
616616
* much entropy as possible, hopefully generating an
617-
* unpredictable and independent uint64_t. Nevertheless
617+
* unpredictable and independent uint64_t. Nevertheless,
618618
* the output of this function MUST NOT be treated as
619619
* being cryptographically safe.
620620
*/

0 commit comments

Comments
 (0)