improper use of rand() w/o RAND_MAX

... with an extra dose of integer modulo thrown in.
This commit is contained in:
Michael Davidsaver
2018-10-18 15:05:31 -07:00
parent 9de8676a6f
commit ac9ebc47ba
3 changed files with 17 additions and 31 deletions

View File

@@ -52,22 +52,22 @@ namespace pvAccess {
// these are byte offset in a CMD_SEARCH request message
// used to mangle a buffer to support incremental construction. (ick!!!)
const int ChannelSearchManager::DATA_COUNT_POSITION = PVA_MESSAGE_HEADER_SIZE + 4+1+3+16+2+1+4;
const int ChannelSearchManager::CAST_POSITION = PVA_MESSAGE_HEADER_SIZE + 4;
const int ChannelSearchManager::PAYLOAD_POSITION = 4;
static const int DATA_COUNT_POSITION = PVA_MESSAGE_HEADER_SIZE + 4+1+3+16+2+1+4;
static const int CAST_POSITION = PVA_MESSAGE_HEADER_SIZE + 4;
static const int PAYLOAD_POSITION = 4;
// 225ms +/- 25ms random
const double ChannelSearchManager::ATOMIC_PERIOD = 0.225;
const int ChannelSearchManager::PERIOD_JITTER_MS = 25;
static const double ATOMIC_PERIOD = 0.225;
static const double PERIOD_JITTER_MS = 0.025;
const int ChannelSearchManager::DEFAULT_USER_VALUE = 1;
const int ChannelSearchManager::BOOST_VALUE = 1;
static const int DEFAULT_USER_VALUE = 1;
static const int BOOST_VALUE = 1;
// must be power of two (so that search is done)
const int ChannelSearchManager::MAX_COUNT_VALUE = 1 << 8;
const int ChannelSearchManager::MAX_FALLBACK_COUNT_VALUE = (1 << 7) + 1;
static const int MAX_COUNT_VALUE = 1 << 8;
static const int MAX_FALLBACK_COUNT_VALUE = (1 << 7) + 1;
const int ChannelSearchManager::MAX_FRAMES_AT_ONCE = 10;
const int ChannelSearchManager::DELAY_BETWEEN_FRAMES_MS = 50;
static const int MAX_FRAMES_AT_ONCE = 10;
static const int DELAY_BETWEEN_FRAMES_MS = 50;
ChannelSearchManager::ChannelSearchManager(Context::shared_pointer const & context) :
@@ -94,7 +94,7 @@ void ChannelSearchManager::activate()
initializeSendBuffer();
// add some jitter so that all the clients do not send at the same time
double period = ATOMIC_PERIOD + (rand() % (2*PERIOD_JITTER_MS+1) - PERIOD_JITTER_MS)/(double)1000;
double period = ATOMIC_PERIOD + double(rand())/RAND_MAX*PERIOD_JITTER_MS;
Context::shared_pointer context(m_context.lock());
if (context)

View File

@@ -170,21 +170,6 @@ private:
* m_channels mutex.
*/
epics::pvData::Mutex m_mutex;
static const int DATA_COUNT_POSITION;
static const int CAST_POSITION;
static const int PAYLOAD_POSITION;
static const double ATOMIC_PERIOD;
static const int PERIOD_JITTER_MS;
static const int DEFAULT_USER_VALUE;
static const int BOOST_VALUE;
static const int MAX_COUNT_VALUE;
static const int MAX_FALLBACK_COUNT_VALUE;
static const int MAX_FRAMES_AT_ONCE;
static const int DELAY_BETWEEN_FRAMES_MS;
};
}

View File

@@ -348,15 +348,16 @@ void ServerSearchHandler::handleResponse(osiSockAddr* responseFrom,
// server discovery ping by pvlist
if (allowed)
{
// ~random hold-off to reduce impact of all servers responding...
#define MAX_SERVER_SEARCH_RESPONSE_DELAY_MS 100
double period = (rand() % MAX_SERVER_SEARCH_RESPONSE_DELAY_MS)/(double)1000;
// ~random hold-off to reduce impact of all servers responding.
// in [0.05, 0.15]
double delay = double(rand())/RAND_MAX; // [0, 1]
delay = delay*0.1 + 0.05;
std::tr1::shared_ptr<ServerChannelFindRequesterImpl> tp(new ServerChannelFindRequesterImpl(_context, 1));
tp->set("", searchSequenceId, 0, responseAddress, true, true);
TimerCallback::shared_pointer tc = tp;
_context->getTimer()->scheduleAfterDelay(tc, period);
_context->getTimer()->scheduleAfterDelay(tc, delay);
}
}
}