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 47f1321 commit ffff7db
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/cpp/threadspecificdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ using namespace LOG4CXX_NS::helpers;
struct ThreadSpecificData::ThreadSpecificDataPrivate{
ThreadSpecificDataPrivate()
: pNamePair(std::make_shared<NamePair>())
, isValid(true)
{
setThreadIdName();
setThreadUserName();
}
~ThreadSpecificDataPrivate()
{
isValid = false;
}
NDC::Stack ndcStack;
MDC::Map mdcMap;

Expand All @@ -59,6 +64,8 @@ struct ThreadSpecificData::ThreadSpecificDataPrivate{

void setThreadIdName();
void setThreadUserName();

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

/* Generate an identifier for the current thread
Expand Down Expand Up @@ -184,7 +191,7 @@ ThreadSpecificData* ThreadSpecificData::getCurrentData()
{
#if LOG4CXX_HAS_THREAD_LOCAL
thread_local ThreadSpecificData data;
return &data;
return data.m_priv && data.m_priv->isValid ? &data : NULL;
#elif APR_HAS_THREADS
void* pData = NULL;
if (APR_SUCCESS == apr_threadkey_private_get(&pData, APRInitializer::getTlsKey())
Expand Down

0 comments on commit ffff7db

Please sign in to comment.