From ed721566dae03a605076f873eae0038c342baa29 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 3 May 2018 12:53:32 +0200 Subject: [PATCH] Merge #13148: logging: Fix potential use-after-free in LogPrintStr(...) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/logging.cpp | 17 ++++++++--------- src/logging.h | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 5cde0f6324cf6c..f8fa4ed6e1f767 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -247,10 +247,8 @@ std::string BCLog::Logger::LogThreadNameStr(const std::string &str) 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 strTimestamped = LogTimestampStr(strThreadLogged); @@ -261,7 +259,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str) if (m_print_to_console) { // print to console - ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); + fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); fflush(stdout); } 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 if (m_fileout == nullptr) { - ret = strTimestamped.length(); m_msgs_before_open.push_back(strTimestamped); } else @@ -277,14 +274,16 @@ int BCLog::Logger::LogPrintStr(const std::string &str) // reopen the log file, if requested if (m_reopen_file) { m_reopen_file = false; - if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr) - setbuf(m_fileout, nullptr); // unbuffered + m_fileout = fsbridge::freopen(m_file_path, "a", m_fileout); + if (!m_fileout) { + return; + } + setbuf(m_fileout, nullptr); // unbuffered } - ret = FileWriteStr(strTimestamped, m_fileout); + FileWriteStr(strTimestamped, m_fileout); } } - return ret; } void BCLog::Logger::ShrinkDebugFile() diff --git a/src/logging.h b/src/logging.h index da4b4208fe9826..1d08ca1cadd9b7 100644 --- a/src/logging.h +++ b/src/logging.h @@ -111,7 +111,7 @@ namespace BCLog { std::atomic m_reopen_file{false}; /** 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 */ bool Enabled() const { return m_print_to_console || m_print_to_file; }