Merge #13148: logging: Fix potential use-after-free in LogPrintStr(...)

0bd4cd3 logging: remove unused return value from LogPrintStr (practicalswift)
76f344d logging: Fix potential use-after-free in LogPrintStr(...) (practicalswift)

Pull request description:

  Fix potential use-after-free in `LogPrintStr(...)`.

  `freopen(…)` frees `m_fileout`.

Tree-SHA512: ceee1f659c10a21525aa648377afeea0a37016339f5269dea54850ba3b475aa316f4931081655717b65f981598fdc9d79a1e79e55f7084c242eeb7bf372bc4b6
This commit is contained in:
Wladimir J. van der Laan 2018-05-03 12:53:32 +02:00 committed by UdjinM6
parent 6423395e2e
commit a03d649a48
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9
2 changed files with 9 additions and 10 deletions

View File

@ -247,10 +247,8 @@ std::string BCLog::Logger::LogThreadNameStr(const std::string &str)
return strThreadLogged; return strThreadLogged;
} }
int BCLog::Logger::LogPrintStr(const std::string &str) void BCLog::Logger::LogPrintStr(const std::string &str)
{ {
int ret = 0; // Returns total number of characters written
std::string strThreadLogged = LogThreadNameStr(str); std::string strThreadLogged = LogThreadNameStr(str);
std::string strTimestamped = LogTimestampStr(strThreadLogged); std::string strTimestamped = LogTimestampStr(strThreadLogged);
@ -261,7 +259,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
if (m_print_to_console) { if (m_print_to_console) {
// print to console // print to console
ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
fflush(stdout); fflush(stdout);
} }
if (m_print_to_file) { if (m_print_to_file) {
@ -269,7 +267,6 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
// buffer if we haven't opened the log yet // buffer if we haven't opened the log yet
if (m_fileout == nullptr) { if (m_fileout == nullptr) {
ret = strTimestamped.length();
m_msgs_before_open.push_back(strTimestamped); m_msgs_before_open.push_back(strTimestamped);
} }
else else
@ -277,14 +274,16 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
// reopen the log file, if requested // reopen the log file, if requested
if (m_reopen_file) { if (m_reopen_file) {
m_reopen_file = false; m_reopen_file = false;
if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr) m_fileout = fsbridge::freopen(m_file_path, "a", m_fileout);
setbuf(m_fileout, nullptr); // unbuffered if (!m_fileout) {
return;
}
setbuf(m_fileout, nullptr); // unbuffered
} }
ret = FileWriteStr(strTimestamped, m_fileout); FileWriteStr(strTimestamped, m_fileout);
} }
} }
return ret;
} }
void BCLog::Logger::ShrinkDebugFile() void BCLog::Logger::ShrinkDebugFile()

View File

@ -111,7 +111,7 @@ namespace BCLog {
std::atomic<bool> m_reopen_file{false}; std::atomic<bool> m_reopen_file{false};
/** Send a string to the log output */ /** Send a string to the log output */
int LogPrintStr(const std::string &str); void LogPrintStr(const std::string &str);
/** Returns whether logs will be written to any output */ /** Returns whether logs will be written to any output */
bool Enabled() const { return m_print_to_console || m_print_to_file; } bool Enabled() const { return m_print_to_console || m_print_to_file; }