1
mirror of https://github.com/monero-project/monero synced 2024-11-16 04:33:51 +01:00

easylogging++: in place log sanitization

saves an object ctor/dtor per log
This commit is contained in:
moneromooo-monero 2021-03-29 22:10:46 +00:00
parent dcba757dd2
commit a16328e853
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3

View File

@ -2477,20 +2477,20 @@ void DefaultLogDispatchCallback::handle(const LogDispatchData* data) {
template<typename Transform> template<typename Transform>
static inline std::string utf8canonical(const std::string &s, Transform t = [](wint_t c)->wint_t { return c; }) static inline void utf8canonical(std::string &s, Transform t = [](wint_t c)->wint_t { return c; })
{ {
std::string sc = "";
size_t avail = s.size(); size_t avail = s.size();
const char *ptr = s.data(); const char *ptr = s.data();
wint_t cp = 0; wint_t cp = 0;
int bytes = 1; int rbytes = 1, bytes = -1;
char wbuf[8], *wptr; char wbuf[8], *wptr;
size_t w_offset = 0;
while (avail--) while (avail--)
{ {
if ((*ptr & 0x80) == 0) if ((*ptr & 0x80) == 0)
{ {
cp = *ptr++; cp = *ptr++;
bytes = 1; rbytes = 1;
} }
else if ((*ptr & 0xe0) == 0xc0) else if ((*ptr & 0xe0) == 0xc0)
{ {
@ -2499,7 +2499,7 @@ static inline std::string utf8canonical(const std::string &s, Transform t = [](w
cp = (*ptr++ & 0x1f) << 6; cp = (*ptr++ & 0x1f) << 6;
cp |= *ptr++ & 0x3f; cp |= *ptr++ & 0x3f;
--avail; --avail;
bytes = 2; rbytes = 2;
} }
else if ((*ptr & 0xf0) == 0xe0) else if ((*ptr & 0xf0) == 0xe0)
{ {
@ -2509,7 +2509,7 @@ static inline std::string utf8canonical(const std::string &s, Transform t = [](w
cp |= (*ptr++ & 0x3f) << 6; cp |= (*ptr++ & 0x3f) << 6;
cp |= *ptr++ & 0x3f; cp |= *ptr++ & 0x3f;
avail -= 2; avail -= 2;
bytes = 3; rbytes = 3;
} }
else if ((*ptr & 0xf8) == 0xf0) else if ((*ptr & 0xf8) == 0xf0)
{ {
@ -2520,7 +2520,7 @@ static inline std::string utf8canonical(const std::string &s, Transform t = [](w
cp |= (*ptr++ & 0x3f) << 6; cp |= (*ptr++ & 0x3f) << 6;
cp |= *ptr++ & 0x3f; cp |= *ptr++ & 0x3f;
avail -= 3; avail -= 3;
bytes = 4; rbytes = 4;
} }
else else
throw std::runtime_error("Invalid UTF-8"); throw std::runtime_error("Invalid UTF-8");
@ -2537,6 +2537,9 @@ static inline std::string utf8canonical(const std::string &s, Transform t = [](w
else else
throw std::runtime_error("Invalid code point UTF-8 transformation"); throw std::runtime_error("Invalid code point UTF-8 transformation");
if (bytes > rbytes)
throw std::runtime_error("In place sanitization requires replacements to not take more space than the original code points");
wptr = wbuf; wptr = wbuf;
switch (bytes) switch (bytes)
{ {
@ -2547,16 +2550,17 @@ static inline std::string utf8canonical(const std::string &s, Transform t = [](w
default: throw std::runtime_error("Invalid UTF-8"); default: throw std::runtime_error("Invalid UTF-8");
} }
*wptr = 0; *wptr = 0;
sc.append(wbuf, bytes); memcpy(&s[w_offset], wbuf, bytes);
w_offset += bytes;
cp = 0; cp = 0;
bytes = 1; bytes = 1;
} }
return sc; s.resize(w_offset);
} }
void sanitize(std::string &s) void sanitize(std::string &s)
{ {
s = utf8canonical(s, [](wint_t c)->wint_t { utf8canonical(s, [](wint_t c)->wint_t {
if (c == 9 || c == 10 || c == 13) if (c == 9 || c == 10 || c == 13)
return c; return c;
if (c < 0x20) if (c < 0x20)