From 8d58d2698ea99593426d5917cbad8646906aa46b Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Tue, 17 Dec 2024 21:05:51 -0800 Subject: [PATCH] Fix potential use before initialization for logging. --- .../CommonFramework/Logging/FileWindowLogger.cpp | 10 +++++++++- .../Source/CommonFramework/Logging/FileWindowLogger.h | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp b/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp index 4d4b4bd39..97d88ba2a 100644 --- a/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp +++ b/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp @@ -48,7 +48,6 @@ FileWindowLogger::FileWindowLogger(const std::string& path) : m_file(QString::fromStdString(path)) , m_max_queue_size(LOG_HISTORY_LINES) , m_stopping(false) - , m_thread(&FileWindowLogger::thread_loop, this) { bool exists = m_file.exists(); m_file.open(QIODevice::WriteOnly | QIODevice::Append); @@ -56,17 +55,22 @@ FileWindowLogger::FileWindowLogger(const std::string& path) std::string bom = "\xef\xbb\xbf"; m_file.write(bom.c_str(), bom.size()); } + + m_thread = std::thread(&FileWindowLogger::thread_loop, this); } void FileWindowLogger::operator+=(FileWindowLoggerWindow& widget){ + auto scope_check = m_sanitizer.check_scope(); std::lock_guard lg(m_lock); m_windows.insert(&widget); } void FileWindowLogger::operator-=(FileWindowLoggerWindow& widget){ + auto scope_check = m_sanitizer.check_scope(); std::lock_guard lg(m_lock); m_windows.erase(&widget); } void FileWindowLogger::log(const std::string& msg, Color color){ + auto scope_check = m_sanitizer.check_scope(); std::unique_lock lg(m_lock); m_last_log_tracker += msg; m_cv.wait(lg, [this]{ return m_queue.size() < m_max_queue_size; }); @@ -74,6 +78,7 @@ void FileWindowLogger::log(const std::string& msg, Color color){ m_cv.notify_all(); } void FileWindowLogger::log(std::string&& msg, Color color){ + auto scope_check = m_sanitizer.check_scope(); std::unique_lock lg(m_lock); m_last_log_tracker += msg; m_cv.wait(lg, [this]{ return m_queue.size() < m_max_queue_size; }); @@ -81,6 +86,7 @@ void FileWindowLogger::log(std::string&& msg, Color color){ m_cv.notify_all(); } std::vector FileWindowLogger::get_last() const{ + auto scope_check = m_sanitizer.check_scope(); std::unique_lock lg(m_lock); return m_last_log_tracker.snapshot(); } @@ -154,6 +160,7 @@ QString FileWindowLogger::to_window_str(const std::string& msg, Color color){ return QString::fromStdString(str); } void FileWindowLogger::internal_log(const std::string& msg, Color color){ + auto scope_check = m_sanitizer.check_scope(); std::string line = normalize_newlines(msg); { if (!m_windows.empty()){ @@ -169,6 +176,7 @@ void FileWindowLogger::internal_log(const std::string& msg, Color color){ } } void FileWindowLogger::thread_loop(){ + auto scope_check = m_sanitizer.check_scope(); std::unique_lock lg(m_lock); while (true){ m_cv.wait(lg, [&]{ diff --git a/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h b/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h index 57c795bf9..b0a391638 100644 --- a/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h +++ b/SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h @@ -15,6 +15,7 @@ #include #include #include +#include "Common/Cpp/LifetimeSanitizer.h" #include "Logger.h" namespace PokemonAutomation{ @@ -66,6 +67,8 @@ class FileWindowLogger : public Logger{ std::deque> m_queue; std::set m_windows; std::thread m_thread; + + LifetimeSanitizer m_sanitizer; };