diff --git a/lib/util.cpp b/lib/util.cpp index 5657e6ed..41ccb313 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -306,11 +306,14 @@ namespace Util{ /// Secure random bytes generator /// Uses /dev/urandom internally void getRandomBytes(void * dest, size_t len){ - static FILE * randSource = fopen("/dev/urandom", "rb"); - if (fread((void *)dest, len, 1, randSource) != 1){ + std::ifstream rndSrc("/dev/urandom", std::ifstream::binary); + rndSrc.read((char*)dest, len); + size_t cnt = rndSrc.gcount(); + if (!rndSrc.good()){ WARN_MSG("Reading random data failed - generating using rand() as backup"); - for (size_t i = 0; i < len; ++i){((char*)dest)[i] = rand() % 255;} + for (size_t i = cnt; i < len; ++i){((char*)dest)[i] = rand() % 255;} } + rndSrc.close(); } /// 64-bits version of ftell diff --git a/src/controller/controller_statistics.cpp b/src/controller/controller_statistics.cpp index a57e6e20..d9a9fabc 100644 --- a/src/controller/controller_statistics.cpp +++ b/src/controller/controller_statistics.cpp @@ -385,7 +385,7 @@ void Controller::SharedMemStats(void *config){ while (((Util::Config *)config)->is_active){ { std::ifstream cpustat("/proc/stat"); - if (cpustat){ + if (cpustat.good()){ char line[300]; while (cpustat.getline(line, 300)){ static uint64_t cl_total = 0, cl_idle = 0; @@ -403,6 +403,7 @@ void Controller::SharedMemStats(void *config){ } } } + cpustat.close(); } { tthread::lock_guard guard(Controller::configMutex);