From bea0edc3f0cb4d683242f4eca0183247a7a5a697 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 29 May 2022 04:46:52 -0300 Subject: [PATCH] Use strncpy instead of strcpy in RageThreads.cpp --- src/RageUtil/Misc/RageThreads.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/RageUtil/Misc/RageThreads.cpp b/src/RageUtil/Misc/RageThreads.cpp index 4a338dd719..c5ecb97db2 100644 --- a/src/RageUtil/Misc/RageThreads.cpp +++ b/src/RageUtil/Misc/RageThreads.cpp @@ -33,13 +33,15 @@ bool RageThread::s_bIsShowingDialog = false; // static std::vector *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; @@ -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,