1
0
Fork 0

Codefix: StartNewThread uses char* after returning

pull/14202/head
Rubidium 2025-05-04 11:18:39 +02:00 committed by rubidium42
parent 8f74c08ea6
commit afc1e76575
6 changed files with 14 additions and 15 deletions

View File

@ -36,7 +36,7 @@ inline bool MacOSVersionIsAtLeast(long major, long minor, long bugfix)
bool IsMonospaceFont(CFStringRef name); bool IsMonospaceFont(CFStringRef name);
void MacOSSetThreadName(const char *name); void MacOSSetThreadName(const std::string &name);
uint64_t MacOSGetPhysicalMemory(); uint64_t MacOSGetPhysicalMemory();

View File

@ -242,15 +242,15 @@ bool IsMonospaceFont(CFStringRef name)
* Set the name of the current thread for the debugger. * Set the name of the current thread for the debugger.
* @param name The new name of the current thread. * @param name The new name of the current thread.
*/ */
void MacOSSetThreadName(const char *name) void MacOSSetThreadName(const std::string &name)
{ {
if (MacOSVersionIsAtLeast(10, 6, 0)) { if (MacOSVersionIsAtLeast(10, 6, 0)) {
pthread_setname_np(name); pthread_setname_np(name.c_str());
} }
NSThread *cur = [ NSThread currentThread ]; NSThread *cur = [ NSThread currentThread ];
if (cur != nil && [ cur respondsToSelector:@selector(setName:) ]) { if (cur != nil && [ cur respondsToSelector:@selector(setName:) ]) {
[ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name ] ]; [ cur performSelector:@selector(setName:) withObject:[ NSString stringWithUTF8String:name.c_str() ] ];
} }
} }

View File

@ -243,12 +243,12 @@ void OSOpenBrowser(const std::string &url)
} }
#endif /* __APPLE__ */ #endif /* __APPLE__ */
void SetCurrentThreadName([[maybe_unused]] const char *threadName) void SetCurrentThreadName([[maybe_unused]] const std::string &thread_name)
{ {
#if defined(__GLIBC__) #if defined(__GLIBC__)
if (threadName) pthread_setname_np(pthread_self(), threadName); pthread_setname_np(pthread_self(), thread_name.c_str());
#endif /* defined(__GLIBC__) */ #endif /* defined(__GLIBC__) */
#if defined(__APPLE__) #if defined(__APPLE__)
MacOSSetThreadName(threadName); MacOSSetThreadName(thread_name);
#endif /* defined(__APPLE__) */ #endif /* defined(__APPLE__) */
} }

View File

@ -520,11 +520,11 @@ PACK_N(struct THREADNAME_INFO {
/** /**
* Signal thread name to any attached debuggers. * Signal thread name to any attached debuggers.
*/ */
void SetCurrentThreadName(const char *threadName) void SetCurrentThreadName(const std::string &thread_name)
{ {
THREADNAME_INFO info; THREADNAME_INFO info;
info.dwType = 0x1000; info.dwType = 0x1000;
info.szName = threadName; info.szName = thread_name.c_str();
info.dwThreadID = -1; info.dwThreadID = -1;
info.dwFlags = 0; info.dwFlags = 0;
@ -537,5 +537,5 @@ void SetCurrentThreadName(const char *threadName)
#pragma warning(pop) #pragma warning(pop)
} }
#else #else
void SetCurrentThreadName(const char *) {} void SetCurrentThreadName(const std::string &) {}
#endif #endif

View File

@ -15,7 +15,6 @@ bool MyShowCursor(bool show, bool toggle = false);
char *convert_from_fs(const std::wstring_view src, std::span<char> dst_buf); char *convert_from_fs(const std::wstring_view src, std::span<char> dst_buf);
wchar_t *convert_to_fs(std::string_view src, std::span<wchar_t> dst_buf); wchar_t *convert_to_fs(std::string_view src, std::span<wchar_t> dst_buf);
void Win32SetCurrentLocaleName(const char *iso_code);
int OTTDStringCompare(std::string_view s1, std::string_view s2); int OTTDStringCompare(std::string_view s1, std::string_view s2);
int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive); int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive);

View File

@ -30,7 +30,7 @@ inline void CSleep(int milliseconds)
* Name the thread this function is called on for the debugger. * Name the thread this function is called on for the debugger.
* @param name Name to set for the thread.. * @param name Name to set for the thread..
*/ */
void SetCurrentThreadName(const char *name); void SetCurrentThreadName(const std::string &name);
/** /**
@ -44,13 +44,13 @@ void SetCurrentThreadName(const char *name);
* @return True if the thread was successfully started, false otherwise. * @return True if the thread was successfully started, false otherwise.
*/ */
template <class TFn, class... TArgs> template <class TFn, class... TArgs>
inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs&&... _Ax) inline bool StartNewThread(std::thread *thr, std::string_view name, TFn&& _Fx, TArgs&&... _Ax)
{ {
try { try {
static std::mutex thread_startup_mutex; static std::mutex thread_startup_mutex;
std::lock_guard<std::mutex> lock(thread_startup_mutex); std::lock_guard<std::mutex> lock(thread_startup_mutex);
std::thread t([] (const char *name, TFn&& F, TArgs&&... A) { std::thread t([] (std::string name, TFn&& F, TArgs&&... A) {
/* Delay starting the thread till the main thread is finished /* Delay starting the thread till the main thread is finished
* with the administration. This prevent race-conditions on * with the administration. This prevent race-conditions on
* startup. */ * startup. */
@ -68,7 +68,7 @@ inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs&
} catch (...) { } catch (...) {
NOT_REACHED(); NOT_REACHED();
} }
}, name, std::forward<TFn>(_Fx), std::forward<TArgs>(_Ax)...); }, std::string{name}, std::forward<TFn>(_Fx), std::forward<TArgs>(_Ax)...);
if (thr != nullptr) { if (thr != nullptr) {
*thr = std::move(t); *thr = std::move(t);