From a16328e85355c8d6f1fc26a19459cc762cbf2587 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 29 Mar 2021 22:10:46 +0000 Subject: easylogging++: in place log sanitization saves an object ctor/dtor per log --- external/easylogging++/easylogging++.cc | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'external') diff --git a/external/easylogging++/easylogging++.cc b/external/easylogging++/easylogging++.cc index bf877c018..862aed360 100644 --- a/external/easylogging++/easylogging++.cc +++ b/external/easylogging++/easylogging++.cc @@ -2477,20 +2477,20 @@ void DefaultLogDispatchCallback::handle(const LogDispatchData* data) { template -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(); const char *ptr = s.data(); wint_t cp = 0; - int bytes = 1; + int rbytes = 1, bytes = -1; char wbuf[8], *wptr; + size_t w_offset = 0; while (avail--) { if ((*ptr & 0x80) == 0) { cp = *ptr++; - bytes = 1; + rbytes = 1; } 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++ & 0x3f; --avail; - bytes = 2; + rbytes = 2; } 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; avail -= 2; - bytes = 3; + rbytes = 3; } 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; avail -= 3; - bytes = 4; + rbytes = 4; } else throw std::runtime_error("Invalid UTF-8"); @@ -2537,6 +2537,9 @@ static inline std::string utf8canonical(const std::string &s, Transform t = [](w else 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; 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"); } *wptr = 0; - sc.append(wbuf, bytes); + memcpy(&s[w_offset], wbuf, bytes); + w_offset += bytes; cp = 0; bytes = 1; } - return sc; + s.resize(w_offset); } 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) return c; if (c < 0x20) -- cgit v1.2.3