From 88852ca161114bfd04d11870f2b34bbdb62f4356 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Tue, 18 Oct 2011 00:34:45 -0400 Subject: [PATCH] Don't crash with a NULL thread name. --- src/thread/beos/SDL_systhread.c | 3 ++- src/thread/pthread/SDL_systhread.c | 8 ++++--- src/thread/windows/SDL_systhread.c | 38 ++++++++++++++++-------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/thread/beos/SDL_systhread.c b/src/thread/beos/SDL_systhread.c index 13e2db823..cf2f577b7 100644 --- a/src/thread/beos/SDL_systhread.c +++ b/src/thread/beos/SDL_systhread.c @@ -66,8 +66,9 @@ int SDL_SYS_CreateThread(SDL_Thread * thread, void *args) { /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */ + const char *threadname = thread->name ? thread->name : "SDL Thread"; char name[B_OS_NAME_LENGTH]; - SDL_snprintf(name, sizeof (name), "%s", thread->name); + SDL_snprintf(name, sizeof (name), "%s", threadname); name[sizeof (name) - 1] = '\0'; /* Create the thread and go! */ diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c index bb4296add..ef4bc2c5a 100644 --- a/src/thread/pthread/SDL_systhread.c +++ b/src/thread/pthread/SDL_systhread.c @@ -82,14 +82,16 @@ SDL_SYS_SetupThread(const char *name) int i; sigset_t mask; + if (name != NULL) { #if ( (__MACOSX__ && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)) || \ (__IPHONEOS__ && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)) ) - if (pthread_setname_np != NULL) { pthread_setname_np(name); } + if (pthread_setname_np != NULL) { pthread_setname_np(name); } #elif HAVE_PTHREAD_SETNAME_NP - pthread_setname_np(pthread_self(), name); + pthread_setname_np(pthread_self(), name); #elif HAVE_PTHREAD_SET_NAME_NP - pthread_set_name_np(pthread_self(), name); + pthread_set_name_np(pthread_self(), name); #endif + } /* Mask asynchronous signals for this thread */ sigemptyset(&mask); diff --git a/src/thread/windows/SDL_systhread.c b/src/thread/windows/SDL_systhread.c index e0bd3fbd1..3c133e09c 100644 --- a/src/thread/windows/SDL_systhread.c +++ b/src/thread/windows/SDL_systhread.c @@ -161,25 +161,27 @@ typedef struct tagTHREADNAME_INFO void SDL_SYS_SetupThread(const char *name) { -#if 0 /* !!! FIXME: __except needs C runtime, which we don't link against. */ -#ifdef _MSC_VER /* !!! FIXME: can we do SEH on other compilers yet? */ - /* This magic tells the debugger to name a thread if it's listening. */ - THREADNAME_INFO inf; - inf.dwType = 0x1000; - inf.szName = name; - inf.dwThreadID = (DWORD) -1; - inf.dwFlags = 0; - - __try - { - RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf); + if (name != NULL) { + #if 0 /* !!! FIXME: __except needs C runtime, which we don't link against. */ + #ifdef _MSC_VER /* !!! FIXME: can we do SEH on other compilers yet? */ + /* This magic tells the debugger to name a thread if it's listening. */ + THREADNAME_INFO inf; + inf.dwType = 0x1000; + inf.szName = name; + inf.dwThreadID = (DWORD) -1; + inf.dwFlags = 0; + + __try + { + RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf); + } + __except(EXCEPTION_CONTINUE_EXECUTION) + { + /* The program itself should ignore this bogus exception. */ + } + #endif + #endif } - __except(EXCEPTION_CONTINUE_EXECUTION) - { - /* The program itself should ignore this bogus exception. */ - } -#endif -#endif } SDL_threadID