utils: Make TimevalToString thread-safe

This commit is contained in:
Peter Keresztes Schmidt 2021-03-07 16:17:17 +01:00
parent 7f9c9c6624
commit 7e86e1ef40
2 changed files with 17 additions and 16 deletions

View File

@ -22,7 +22,7 @@
#include "zm_config.h" #include "zm_config.h"
#include "zm_logger.h" #include "zm_logger.h"
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <array>
#include <cstring> #include <cstring>
#include <fcntl.h> /* Definition of AT_* constants */ #include <fcntl.h> /* Definition of AT_* constants */
#include <sstream> #include <sstream>
@ -43,7 +43,7 @@ std::string trimSet(std::string str, std::string trimset) {
// Trim Both leading and trailing sets // Trim Both leading and trailing sets
size_t startpos = str.find_first_not_of(trimset); // Find the first character position after excluding leading blank spaces size_t startpos = str.find_first_not_of(trimset); // Find the first character position after excluding leading blank spaces
size_t endpos = str.find_last_not_of(trimset); // Find the first character position from reverse af size_t endpos = str.find_last_not_of(trimset); // Find the first character position from reverse af
// if all spaces or empty return an empty string // if all spaces or empty return an empty string
if ( ( std::string::npos == startpos ) || ( std::string::npos == endpos ) ) if ( ( std::string::npos == startpos ) || ( std::string::npos == endpos ) )
return std::string(""); return std::string("");
@ -148,7 +148,7 @@ const std::string base64Encode(const std::string &inString) {
selection = remainder | (*inPtr >> 4); selection = remainder | (*inPtr >> 4);
remainder = (*inPtr++ & 0x0f) << 2; remainder = (*inPtr++ & 0x0f) << 2;
outString += base64_table[selection]; outString += base64_table[selection];
if ( *inPtr ) { if ( *inPtr ) {
selection = remainder | (*inPtr >> 6); selection = remainder | (*inPtr >> 6);
outString += base64_table[selection]; outString += base64_table[selection];
@ -175,7 +175,7 @@ int split(const char* string, const char delim, std::vector<std::string>& items)
return -2; return -2;
std::string str(string); std::string str(string);
while ( true ) { while ( true ) {
size_t pos = str.find(delim); size_t pos = str.find(delim);
items.push_back(str.substr(0, pos)); items.push_back(str.substr(0, pos));
@ -242,7 +242,7 @@ void hwcaps_detect() {
} else { } else {
sse_version = 0; sse_version = 0;
Debug(1, "Detected a x86\\x86-64 processor"); Debug(1, "Detected a x86\\x86-64 processor");
} }
#elif defined(__arm__) #elif defined(__arm__)
// ARM processor in 32bit mode // ARM processor in 32bit mode
// To see if it supports NEON, we need to get that information from the kernel // To see if it supports NEON, we need to get that information from the kernel
@ -279,7 +279,7 @@ void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes) {
"sse2_copy_iter:\n\t" "sse2_copy_iter:\n\t"
"movdqa (%0),%%xmm0\n\t" "movdqa (%0),%%xmm0\n\t"
"movdqa 0x10(%0),%%xmm1\n\t" "movdqa 0x10(%0),%%xmm1\n\t"
"movdqa 0x20(%0),%%xmm2\n\t" "movdqa 0x20(%0),%%xmm2\n\t"
"movdqa 0x30(%0),%%xmm3\n\t" "movdqa 0x30(%0),%%xmm3\n\t"
"movdqa 0x40(%0),%%xmm4\n\t" "movdqa 0x40(%0),%%xmm4\n\t"
"movdqa 0x50(%0),%%xmm5\n\t" "movdqa 0x50(%0),%%xmm5\n\t"
@ -328,16 +328,17 @@ void timespec_diff(struct timespec *start, struct timespec *end, struct timespec
} }
} }
char *timeval_to_string( struct timeval tv ) { std::string TimevalToString(timeval tv) {
time_t nowtime; tm now = {};
struct tm *nowtm; std::array<char, 26> tm_buf = {};
static char tmbuf[20], buf[28];
nowtime = tv.tv_sec; localtime_r(&tv.tv_sec, &now);
nowtm = localtime(&nowtime); size_t tm_buf_len = strftime(tm_buf.data(), tm_buf.size(), "%Y-%m-%d %H:%M:%S", &now);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm); if (tm_buf_len == 0) {
snprintf(buf, sizeof buf-1, "%s.%06ld", tmbuf, tv.tv_usec); return "";
return buf; }
return stringtf("%s.%06ld", tm_buf.data(), tv.tv_usec);
} }
std::string UriDecode( const std::string &encoded ) { std::string UriDecode( const std::string &encoded ) {

View File

@ -63,7 +63,7 @@ void hwcaps_detect();
extern unsigned int sse_version; extern unsigned int sse_version;
extern unsigned int neonversion; extern unsigned int neonversion;
char *timeval_to_string( struct timeval tv ); std::string TimevalToString(timeval tv);
std::string UriDecode( const std::string &encoded ); std::string UriDecode( const std::string &encoded );
void touch( const char *pathname ); void touch( const char *pathname );