From 2fc8bdb9714f02430a5850f61263a919d69aad0e Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Wed, 6 Nov 2024 17:37:23 +1100 Subject: [PATCH] Guard against a null return from ThreadSpecificData::getCurrentData() --- src/main/cpp/loggingevent.cpp | 21 ++++++++++++--------- src/main/cpp/threadspecificdata.cpp | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/cpp/loggingevent.cpp b/src/main/cpp/loggingevent.cpp index 0fc0dacff..53d1b810e 100644 --- a/src/main/cpp/loggingevent.cpp +++ b/src/main/cpp/loggingevent.cpp @@ -38,7 +38,7 @@ using namespace LOG4CXX_NS::helpers; struct LoggingEvent::LoggingEventPrivate { - LoggingEventPrivate(const ThreadSpecificData::NamePairPtr& p = ThreadSpecificData::getNames()) : + LoggingEventPrivate(const ThreadSpecificData::NamePairPtr p = ThreadSpecificData::getNames()) : timeStamp(0), #if LOG4CXX_ABI_VERSION <= 15 threadName(p->idString), @@ -53,7 +53,7 @@ struct LoggingEvent::LoggingEventPrivate , const LevelPtr& level1 , const LocationInfo& locationInfo1 , LogString&& message1 - , const ThreadSpecificData::NamePairPtr& p = ThreadSpecificData::getNames() + , const ThreadSpecificData::NamePairPtr p = ThreadSpecificData::getNames() ) : logger(logger1), level(level1), @@ -243,19 +243,22 @@ LoggingEvent::KeySet LoggingEvent::getMDCKeySet() const for (auto const& item : m_priv->dc->map) result.push_back(item.first); } - else for (auto const& item : ThreadSpecificData::getCurrentData()->getMap()) - result.push_back(item.first); + else if (auto pData = ThreadSpecificData::getCurrentData()) + for (auto const& item : pData->getMap()) + result.push_back(item.first); return result; } void LoggingEvent::LoadDC() const { m_priv->dc = std::make_unique(); - auto pData = ThreadSpecificData::getCurrentData(); - m_priv->dc->map = pData->getMap(); - auto& stack = pData->getStack(); - if (!stack.empty()) - m_priv->dc->ctx = stack.top(); + if (auto pData = ThreadSpecificData::getCurrentData()) + { + m_priv->dc->map = pData->getMap(); + auto& stack = pData->getStack(); + if (!stack.empty()) + m_priv->dc->ctx = stack.top(); + } } #if LOG4CXX_ABI_VERSION <= 15 diff --git a/src/main/cpp/threadspecificdata.cpp b/src/main/cpp/threadspecificdata.cpp index fdd92e773..3eafce02e 100644 --- a/src/main/cpp/threadspecificdata.cpp +++ b/src/main/cpp/threadspecificdata.cpp @@ -157,7 +157,8 @@ MDC::Map& ThreadSpecificData::getMap() auto ThreadSpecificData::getNames() -> NamePairPtr { - return getCurrentData()->m_priv->pNamePair; + auto p = getCurrentData(); + return p ? p->m_priv->pNamePair : std::make_shared(); } #if !LOG4CXX_LOGCHAR_IS_UNICHAR && !LOG4CXX_LOGCHAR_IS_WCHAR