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".
This commit is contained in:
Matthias Clasen 2024-10-28 21:49:21 -04:00 committed by Philip Withnall
parent 9949067a69
commit 5b84636e62

View File

@ -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)