Skip to content

Commit

Permalink
Use strncpy instead of strcpy in RageThreads.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed May 29, 2022
1 parent e471c52 commit bea0edc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/RageUtil/Misc/RageThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ bool RageThread::s_bIsShowingDialog = false;
// static std::vector<RageMutex*> *g_MutexList = NULL; /* watch out for static
// initialization order problems */

constexpr size_t MAX_THREAD_NAME_LEN = 1024;

struct ThreadSlot
{
mutable char m_szName[1024]; /* mutable so we can force nul-termination */
mutable char m_szName[MAX_THREAD_NAME_LEN]; /* mutable so we can force nul-termination */

/* Format this beforehand, since it's easier to do that than to do it under
* crash conditions. */
char m_szThreadFormattedOutput[1024];
char m_szThreadFormattedOutput[MAX_THREAD_NAME_LEN];

bool m_bUsed{ false };
uint64_t m_iID;
Expand Down Expand Up @@ -262,10 +264,12 @@ RageThread::Create(int (*fn)(void*), void* data)
const int slotno = FindEmptyThreadSlot();
m_pSlot = &g_ThreadSlots[slotno];

strcpy(m_pSlot->m_szName, m_sName.c_str());
strncpy(m_pSlot->m_szName, m_sName.c_str(), MAX_THREAD_NAME_LEN - 1);
m_pSlot->m_szName[MAX_THREAD_NAME_LEN - 1] = '\0';

Locator::getLogger()->info("Starting thread: {}", m_sName.c_str());
sprintf(m_pSlot->m_szThreadFormattedOutput, "Thread: %s", m_sName.c_str());
snprintf(m_pSlot->m_szThreadFormattedOutput, MAX_THREAD_NAME_LEN - 1, "Thread: %s", m_sName.c_str());
m_pSlot->m_szThreadFormattedOutput[MAX_THREAD_NAME_LEN - 1] = '\0';

/* Start a thread using our own startup function. We pass the id to fill
* in, to make sure it's set before the thread actually starts. (Otherwise,
Expand Down

0 comments on commit bea0edc

Please sign in to comment.