From 5b84636e62d2c77ad4fee49742750a3df762736c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 28 Oct 2024 21:49:21 -0400 Subject: [PATCH] thread: Force-limit thread name length The documentation for glibc's pthread_setname_np states: The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the terminating null byte ('\0'). The documentation for Solaris' pthread_setname_np states: The thread name is a string of length 31 bytes or less, UTF-8 encoded. Failing to respect this length limitation may lead to no name being set, which is confusing, since the thread then shows up under the binary name in gdb. This was happening for the pango worker thread with the name "[pango] fontconfig". --- glib/gthread-posix.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c index 4646ec8e5..fcd005c37 100644 --- a/glib/gthread-posix.c +++ b/glib/gthread-posix.c @@ -812,7 +812,14 @@ g_system_thread_set_name (const gchar *name) #if defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) pthread_setname_np (name); /* on OS X and iOS */ #elif defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) - pthread_setname_np (pthread_self (), name); /* on Linux and Solaris */ +#ifdef __LINUX__ +#define MAX_THREADNAME_LEN 16 +#else +#define MAX_THREADNAME_LEN 32 +#endif + char name_[MAX_THREADNAME_LEN]; + g_strlcpy (name_, name, MAX_THREADNAME_LEN); + pthread_setname_np (pthread_self (), name_); /* on Linux and Solaris */ #elif defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG) pthread_setname_np (pthread_self (), "%s", (gchar *) name); /* on NetBSD */ #elif defined(HAVE_PTHREAD_SET_NAME_NP)