From c747416071cefc26aad71e0a6a585747b81d7d9f Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 29 Dec 2020 13:44:13 +0100 Subject: [PATCH] Changed from localtime to the more secure localtime_r and localtime_s functions. --- es-core/src/Log.cpp | 10 +++++++++- es-core/src/utils/TimeUtil.cpp | 13 +++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/es-core/src/Log.cpp b/es-core/src/Log.cpp index fe6585e81..e94480548 100644 --- a/es-core/src/Log.cpp +++ b/es-core/src/Log.cpp @@ -54,7 +54,15 @@ void Log::open() std::ostringstream& Log::get(LogLevel level) { time_t t = time(nullptr); - os << std::put_time(localtime(&t), "%b %d %T ") << logLevelMap[level] << ":\t"; + struct tm tm; + #if defined(_WIN64) + // Of course Windows does not follow standards and puts the parameters the other way + // around compared to POSIX. + localtime_s(&tm, &t); + #else + localtime_r(&t, &tm); + #endif + os << std::put_time(&tm, "%b %d %T ") << logLevelMap[level] << ":\t"; messageLevel = level; return os; diff --git a/es-core/src/utils/TimeUtil.cpp b/es-core/src/utils/TimeUtil.cpp index 10c79cf38..d4b5e87f9 100644 --- a/es-core/src/utils/TimeUtil.cpp +++ b/es-core/src/utils/TimeUtil.cpp @@ -44,7 +44,11 @@ namespace Utils void DateTime::setTime(const time_t& _time) { mTime = (_time < 0) ? 0 : _time; - mTimeStruct = *localtime(&mTime); + #if defined(_WIN64) + localtime_s(&mTimeStruct, &mTime); + #else + localtime_r(&mTime, &mTimeStruct); + #endif mIsoString = timeToString(mTime); } @@ -171,7 +175,12 @@ namespace Utils std::string timeToString(const time_t& _time, const std::string& _format) { const char* f = _format.c_str(); - const tm timeStruct = *localtime(&_time); + tm timeStruct; + #if defined(_WIN64) + localtime_s(&timeStruct, &_time); + #else + localtime_r(&_time, &timeStruct); + #endif char buf[256] = { '\0' }; char* s = buf;