From a3c05bd7935a83b70e0ed6cccb50d0916ba4cf3d Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Mon, 6 Jan 2025 15:00:05 +0100 Subject: [PATCH] base/thread.h: guard deref of '_logger()' The pointer returned by '_logger()' can be a nullptr, in particular while tracing is (temporarily) inhibited. This patch ensures that the 'Thread::trace' accessors never operate on a nullptr. Fixes #5410 --- repos/base/include/base/thread.h | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/repos/base/include/base/thread.h b/repos/base/include/base/thread.h index af3c86d929..0ae686895d 100644 --- a/repos/base/include/base/thread.h +++ b/repos/base/include/base/thread.h @@ -151,6 +151,12 @@ class Genode::Thread */ static Trace::Logger *_logger(); + static void _with_logger(auto const &fn) + { + Trace::Logger * const ptr = _logger(); + if (ptr) fn(*ptr); + } + /** * Hook for platform-specific constructor supplements * @@ -397,14 +403,6 @@ class Genode::Thread */ void join(); - /** - * Log null-terminated string as trace event - */ - static void trace(char const *cstring) - { - _logger()->log(cstring, strlen(cstring)); - } - /** * Log null-terminated string as trace event using log_output policy * @@ -412,7 +410,10 @@ class Genode::Thread */ static bool trace_captured(char const *cstring) { - return _logger()->log_captured(cstring, strlen(cstring)); + bool result = false; + _with_logger([&] (Trace::Logger &l) { + result = l.log_captured(cstring, strlen(cstring)); }); + return result; } /** @@ -420,13 +421,21 @@ class Genode::Thread */ static void trace(char const *data, size_t len) { - _logger()->log(data, len); + _with_logger([&] (Trace::Logger &l) { l.log(data, len); }); } + /** + * Log null-terminated string as trace event + */ + static void trace(char const *cstring) { trace(cstring, strlen(cstring)); } + /** * Log trace event as defined in base/trace/events.h */ - static void trace(auto const *event) { _logger()->log(event); } + static void trace(auto const *event) + { + _with_logger([&] (Trace::Logger &l) { l.log(event); }); + } /** * Thread affinity