mirror of
https://github.com/transmission/transmission
synced 2025-01-30 19:03:04 +00:00
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:
parent
a82e728ad5
commit
d7f2dee4e0
1 changed files with 3 additions and 9 deletions
|
@ -89,17 +89,11 @@ int tr_rand_int(int upper_bound)
|
||||||
{
|
{
|
||||||
TR_ASSERT(upper_bound > 0);
|
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;
|
return noise % upper_bound;
|
||||||
|
|
||||||
/* abs(INT_MIN) is undefined and could return negative value */
|
|
||||||
if (noise >= 0)
|
|
||||||
{
|
|
||||||
return noise;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fall back to a weaker implementation... */
|
/* fall back to a weaker implementation... */
|
||||||
|
|
Loading…
Reference in a new issue