SDL: Fixed bug #5283 - limit thread name to 16 characters when using pthread_setname_np()

From 9e46a512b15a1d096858e8f15e121b6d59f2129b Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Wed, 2 Feb 2022 09:58:15 +0100
Subject: [PATCH] Fixed bug #5283 - limit thread name to 16 characters when
 using pthread_setname_np()

---
 src/thread/pthread/SDL_systhread.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c
index 41184de5203..fb68b24ec04 100644
--- a/src/thread/pthread/SDL_systhread.c
+++ b/src/thread/pthread/SDL_systhread.c
@@ -41,6 +41,8 @@
 #include "../../core/linux/SDL_dbus.h"
 #endif /* __LINUX__ */
 
+#undef HAVE_DLOPEN
+
 #if (defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)) && defined(HAVE_DLOPEN)
 #include <dlfcn.h>
 #ifndef RTLD_DEFAULT
@@ -137,22 +139,25 @@ SDL_SYS_SetupThread(const char *name)
             #if defined(__MACOSX__) || defined(__IPHONEOS__)
             ppthread_setname_np(name);
             #elif defined(__LINUX__)
-            ppthread_setname_np(pthread_self(), name);
+            char namebuf[16]; /* Limited to 16 char */
+            SDL_strlcpy(namebuf, name, sizeof (namebuf));
+            ppthread_setname_np(pthread_self(), namebuf);
             #endif
         }
         #elif HAVE_PTHREAD_SETNAME_NP
             #if defined(__NETBSD__)
             pthread_setname_np(pthread_self(), "%s", name);
             #else
-            pthread_setname_np(pthread_self(), name);
+            char namebuf[16]; /* Limited to 16 char */
+            SDL_strlcpy(namebuf, name, sizeof (namebuf));
+            pthread_setname_np(pthread_self(), namebuf);
             #endif
         #elif HAVE_PTHREAD_SET_NAME_NP
             pthread_set_name_np(pthread_self(), name);
         #elif defined(__HAIKU__)
             /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
             char namebuf[B_OS_NAME_LENGTH];
-            SDL_snprintf(namebuf, sizeof (namebuf), "%s", name);
-            namebuf[sizeof (namebuf) - 1] = '\0';
+            SDL_strlcpy(namebuf, name, sizeof (namebuf));
             rename_thread(find_thread(NULL), namebuf);
         #endif
     }