mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
util: Filter control characters out of log messages
Belts and suspenders: make sure outgoing log messages don't contain
potentially suspicious characters, such as terminal control codes.
This escapes control characters except newline ('\n') in C syntax.
It escapes instead of removes them to still allow for troubleshooting
issues where they accidentally end up in strings.
This commit is contained in:
@@ -224,10 +224,32 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
|
||||
return strStamped;
|
||||
}
|
||||
|
||||
namespace BCLog {
|
||||
/** Belts and suspenders: make sure outgoing log messages don't contain
|
||||
* potentially suspicious characters, such as terminal control codes.
|
||||
*
|
||||
* This escapes control characters except newline ('\n') in C syntax.
|
||||
* It escapes instead of removes them to still allow for troubleshooting
|
||||
* issues where they accidentally end up in strings.
|
||||
*/
|
||||
std::string LogEscapeMessage(const std::string& str) {
|
||||
std::string ret;
|
||||
for (char ch_in : str) {
|
||||
uint8_t ch = (uint8_t)ch_in;
|
||||
if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
|
||||
ret += ch_in;
|
||||
} else {
|
||||
ret += strprintf("\\x%02x", ch);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
void BCLog::Logger::LogPrintStr(const std::string& str)
|
||||
{
|
||||
std::lock_guard<std::mutex> scoped_lock(m_cs);
|
||||
std::string str_prefixed = str;
|
||||
std::string str_prefixed = LogEscapeMessage(str);
|
||||
|
||||
if (m_log_threadnames && m_started_new_line) {
|
||||
str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] ");
|
||||
|
||||
Reference in New Issue
Block a user