From 31ac4e604a420bbefcb4e7bfc96cc77a451ef2fc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 26 Jul 2007 03:57:45 +0000 Subject: [PATCH] a better fix for the tr_rand() crash experienced earlier today --- libtransmission/peer.c | 2 +- libtransmission/utils.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/libtransmission/peer.c b/libtransmission/peer.c index 6b7bda971..6a07d061d 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -685,7 +685,7 @@ writeEnd: all peers for 2, and so on. Randomize our starting point into "pool" to reduce such overlap */ int piecesLeft = poolSize; - int p = (int)(tr_date() % poolSize); + int p = piecesLeft ? tr_rand(piecesLeft) : 0; for( ; openSlots>0 && piecesLeft>0; --piecesLeft, p=(p+1)%poolSize ) { const int piece = pool[p]; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index c9a40245c..d6d9f1170 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -168,6 +168,9 @@ void tr_msg( int level, char * msg, ... ) int tr_rand( int sup ) { static int init = 0; + + assert( sup > 0 ); + if( !init ) { srand( tr_date() );