From ccb7b056d604348f072e77d9b134a3f47e983fcb Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 15 Jul 2023 12:56:38 +0100 Subject: Replace rand/rand_s/random with the C++11 random number generator. --- src/helperfuncs.cpp | 22 ++++++---------------- src/inspircd.cpp | 10 ---------- 2 files changed, 6 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 203c5de04..f3a994007 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -28,10 +28,7 @@ */ -#ifdef _WIN32 -#define _CRT_RAND_S -#include -#endif +#include #include "inspircd.h" #include "timeutils.h" @@ -464,17 +461,10 @@ void InspIRCd::DefaultGenRandom(char* output, size_t max) #if defined HAS_ARC4RANDOM_BUF arc4random_buf(output, max); #else - for (unsigned int i = 0; i < max; ++i) -# ifdef _WIN32 - { - unsigned int uTemp; - if(rand_s(&uTemp) != 0) - output[i] = rand(); - else - output[i] = uTemp; - } -# else - output[i] = random(); -# endif + static std::random_device device; + static std::mt19937 engine(device()); + static std::uniform_int_distribution dist; + for (size_t i = 0; i < max; ++i) + output[i] = dist(engine); #endif } diff --git a/src/inspircd.cpp b/src/inspircd.cpp index a4c6c9edc..c6aab7dac 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -341,15 +341,6 @@ namespace ServerInstance->Config->CommandLine.writelog = !do_nolog; ServerInstance->Config->CommandLine.writepid = !do_nopid; } - // Seeds the random number generator if applicable. - void SeedRng(timespec ts) - { -#if defined _WIN32 - srand(ts.tv_nsec ^ ts.tv_sec); -#elif !defined HAS_ARC4RANDOM_BUF - srandom(static_cast(ts.tv_nsec ^ ts.tv_sec)); -#endif - } // Sets handlers for various process signals. void SetSignals() @@ -468,7 +459,6 @@ InspIRCd::InspIRCd(int argc, char** argv) this->startup_time = TIME.tv_sec; IncreaseCoreDumpSize(); - SeedRng(TIME); SocketEngine::Init(); this->Config = new ServerConfig; -- cgit v1.3.1-10-gc9f91