crypto-utils: do tr_rand_int without abs (#1198)

* crypto-utils: do tr_rand_int without abs

There is really not much point in trying to abs a random int when you can just interpret it as unsigned in the first place.

* fixup! trailing space

how did this get in here?

* fixup! unsigned int

Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
Mingye Wang 2020-05-12 09:34:03 +08:00 committed by GitHub
parent a82e728ad5
commit d7f2dee4e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 9 deletions

View File

@ -89,17 +89,11 @@ int tr_rand_int(int upper_bound)
{
TR_ASSERT(upper_bound > 0);
int noise;
unsigned int noise;
while (tr_rand_buffer(&noise, sizeof(noise)))
if (tr_rand_buffer(&noise, sizeof(noise)))
{
noise = abs(noise) % upper_bound;
/* abs(INT_MIN) is undefined and could return negative value */
if (noise >= 0)
{
return noise;
}
return noise % upper_bound;
}
/* fall back to a weaker implementation... */