From 979eddf61dfdee5ab3ca23c243b5e0cf6dbf59b7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 19 May 2009 20:52:24 +0000 Subject: [PATCH] (trunk libT) add some extra tests to tr_cryptoWeakRandInt() --- libtransmission/crypto.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libtransmission/crypto.c b/libtransmission/crypto.c index 6257b74dc..1b85450ca 100644 --- a/libtransmission/crypto.c +++ b/libtransmission/crypto.c @@ -311,6 +311,8 @@ tr_cryptoRandInt( int upperBound ) int noise; int val; + assert( upperBound > 0 ); + if( RAND_pseudo_bytes ( (unsigned char *) &noise, sizeof noise ) >= 0 ) { val = abs( noise ) % upperBound; @@ -328,17 +330,22 @@ tr_cryptoRandInt( int upperBound ) int tr_cryptoWeakRandInt( int upperBound ) { - static int init = 0; + int val; + static tr_bool init = FALSE; assert( upperBound > 0 ); if( !init ) { srand( tr_date( ) ); - init = 1; + init = TRUE; } - return rand( ) % upperBound; + + val = rand( ) % upperBound; + assert( val >= 0 ); + assert( val < upperBound ); + return val; } void