Replace snprintf with string and ostringstream (#1649)

These uses are probably safe from a buffer overflow perspective but
can cause data race issues in logging due to static buffers.
This commit is contained in:
Andrew Gaul
2021-05-08 02:48:47 +09:00
committed by GitHub
parent 096a230b70
commit bb6d2b1b74
5 changed files with 33 additions and 41 deletions

View File

@ -23,6 +23,7 @@
#include <cstring>
#include <cerrno>
#include <climits>
#include <iomanip>
#include <stdexcept>
#include <sstream>
@ -59,9 +60,7 @@ template<> std::string str(struct timespec value) {
std::ostringstream s;
s << value.tv_sec;
if(value.tv_nsec != 0){
char buf[16];
snprintf(buf, sizeof(buf), "%09lld", static_cast<long long>(value.tv_nsec));
s << "." << buf;
s << "." << std::setfill('0') << std::setw(9) << value.tv_nsec;
}
return s.str();
}