From 13e36df7c89e1f4b51fa1a4356e16a16114c7f1b Mon Sep 17 00:00:00 2001 From: Julien Letessier Date: Thu, 15 Jan 2026 13:00:16 +0100 Subject: [PATCH] Initialize thread pointer in logger --- src/Logger.cpp | 1 + test/logger_thread_init.cpp | 11 +++++++++ test/logger_thread_init.py | 47 +++++++++++++++++++++++++++++++++++++ test/run.sh | 1 + 4 files changed, 60 insertions(+) create mode 100644 test/logger_thread_init.cpp create mode 100644 test/logger_thread_init.py diff --git a/src/Logger.cpp b/src/Logger.cpp index ced0e53..22b92f0 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -87,6 +87,7 @@ Logger::Logger(int maxLogUnitNum): mLogUnitCnt(0), mLogs(maxLogUnitNum), mFree(maxLogUnitNum), + mThread(nullptr), mFileSink(nullptr) { mLogs.resize(0); diff --git a/test/logger_thread_init.cpp b/test/logger_thread_init.cpp new file mode 100644 index 0000000..4883f1f --- /dev/null +++ b/test/logger_thread_init.cpp @@ -0,0 +1,11 @@ +/* + * Ensure Logger destructor is safe when thread was never started. + */ + +#include "../src/Logger.h" + +int main() { + Logger* logger = new Logger(); + delete logger; + return 0; +} diff --git a/test/logger_thread_init.py b/test/logger_thread_init.py new file mode 100644 index 0000000..8f395dd --- /dev/null +++ b/test/logger_thread_init.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# +# Build and run the Logger thread init test. +# + +import os +import subprocess +import tempfile +from test_util import parse_args, exit_with_result + + +def run_test(project_root): + src = os.path.join(project_root, "test", "logger_thread_init.cpp") + if not os.path.exists(src): + print("FAIL: logger_thread_init.cpp not found") + return False + + with tempfile.TemporaryDirectory() as tmp: + exe = os.path.join(tmp, "logger_thread_init") + cmd = [ + "g++", + "-std=c++11", + src, + os.path.join(project_root, "src", "Logger.cpp"), + os.path.join(project_root, "src", "LogFileSink.cpp"), + os.path.join(project_root, "src", "Timer.cpp"), + "-o", + exe, + ] + try: + subprocess.check_call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + except Exception as exc: + print("FAIL: compile logger_thread_init:", exc) + return False + try: + subprocess.check_call([exe], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + except subprocess.CalledProcessError as exc: + print("FAIL: logger_thread_init returned", exc.returncode) + return False + return True + + +if __name__ == "__main__": + _ = parse_args("Logger thread init test") + root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + success = run_test(root) + exit_with_result(success, "logger thread init", "logger thread init") diff --git a/test/run.sh b/test/run.sh index d2baf7b..2e00391 100755 --- a/test/run.sh +++ b/test/run.sh @@ -147,6 +147,7 @@ TESTS=( "test/signal_handling.py" "test/buffer_vsnprintf.py" "test/logunit_vsnprintf.py" + "test/logger_thread_init.py" "test/pubsub_long_name.py" "test/pubsub_large_message.py" "test/transaction_forbid.py"