Skip to content

Commit

Permalink
Prevent use of thread specific data after destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Nov 6, 2024
1 parent 3cc9e94 commit 00d73bf
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/main/cpp/threadspecificdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ using namespace LOG4CXX_NS::helpers;
struct ThreadSpecificData::ThreadSpecificDataPrivate{
ThreadSpecificDataPrivate()
: pNamePair(std::make_shared<NamePair>())
, isValid(true)
, isValid(true)
{
setThreadIdName();
setThreadUserName();
}
~ThreadSpecificDataPrivate()
{
isValid = false;
}
~ThreadSpecificDataPrivate()
{
isValid = false;
}
NDC::Stack ndcStack;
MDC::Map mdcMap;

Expand All @@ -65,7 +65,7 @@ struct ThreadSpecificData::ThreadSpecificDataPrivate{
void setThreadIdName();
void setThreadUserName();

bool isValid; // Is this object un-destructed?
bool isValid; // Is this object un-destructed?
};

/* Generate an identifier for the current thread
Expand Down Expand Up @@ -149,7 +149,7 @@ ThreadSpecificData::ThreadSpecificData(ThreadSpecificData&& other)

ThreadSpecificData::~ThreadSpecificData()
{
m_priv.reset();
m_priv.reset();
}

NDC::Stack& ThreadSpecificData::getStack()
Expand Down Expand Up @@ -238,12 +238,16 @@ void ThreadSpecificData::recycle()

void ThreadSpecificData::put(const LogString& key, const LogString& val)
{
getCurrentData()->getMap()[key] = val;
if (auto p = getCurrentData())
p->getMap()[key] = val;
}

void ThreadSpecificData::push(const LogString& val)
{
NDC::Stack& stack = getCurrentData()->getStack();
auto p = getCurrentData();
if (!p)
return;
NDC::Stack& stack = p->getStack();
if (stack.empty())
{
stack.push(NDC::DiagnosticContext(val, val));
Expand All @@ -259,6 +263,7 @@ void ThreadSpecificData::push(const LogString& val)

void ThreadSpecificData::inherit(const NDC::Stack& src)
{
getCurrentData()->getStack() = src;
if (auto p = getCurrentData())
p->getStack() = src;
}

0 comments on commit 00d73bf

Please sign in to comment.