Skip to content

Commit

Permalink
Guard against a null return from ThreadSpecificData::getCurrentData()
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Nov 6, 2024
1 parent 2a01934 commit 2fc8bdb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/main/cpp/loggingevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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<LoggingEventPrivate::DiagnosticContext>();
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
Expand Down
3 changes: 2 additions & 1 deletion src/main/cpp/threadspecificdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<NamePair>();
}

#if !LOG4CXX_LOGCHAR_IS_UNICHAR && !LOG4CXX_LOGCHAR_IS_WCHAR
Expand Down

0 comments on commit 2fc8bdb

Please sign in to comment.