app-layer-htp, stream-tcp: prevent modulo bias in RandomGetWrap()

RAND_MAX is not guaranteed to be a divisor of ULONG_MAX, so take the
necessary precautions to get unbiased random numbers. Although the
bias might be negligible, it's not advisable to rely on it.
pull/3183/head
Martin Natano 8 years ago committed by Victor Julien
parent 2e4305f504
commit 18f64e0d21

@ -2215,9 +2215,13 @@ static void HTPConfigSetDefaultsPhase1(HTPCfgRec *cfg_prec)
*/
static int RandomGetWrap(void)
{
long int r = RandomGet();
int r_int = r % (long int)RAND_MAX;
return abs(r_int);
unsigned long r;
do {
r = RandomGet();
} while(r >= ULONG_MAX - (ULONG_MAX % RAND_MAX));
return r % RAND_MAX;
}
/*

@ -344,9 +344,13 @@ int StreamTcpInlineDropInvalid(void)
*/
static int RandomGetWrap(void)
{
long int r = RandomGet();
int r_int = r % (long int)RAND_MAX;
return abs(r_int);
unsigned long r;
do {
r = RandomGet();
} while(r >= ULONG_MAX - (ULONG_MAX % RAND_MAX));
return r % RAND_MAX;
}
/** \brief To initialize the stream global configuration data

Loading…
Cancel
Save