From dcafc9ea8629bf433dd68e3ebc22b52d7d71295e Mon Sep 17 00:00:00 2001 From: "wtc%google.com" Date: Fri, 22 Jun 2012 01:36:02 +0000 Subject: [PATCH] Bug 758837: add a comment to explain the source of the 15-char name length limit. Copy the null terminator from the input thread name. r=honzab. git-svn-id: svn://10.0.0.236/trunk@263974 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/nsprpub/pr/src/pthreads/ptthread.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mozilla/nsprpub/pr/src/pthreads/ptthread.c b/mozilla/nsprpub/pr/src/pthreads/ptthread.c index 9b98dd25c54..d78dafabf4b 100644 --- a/mozilla/nsprpub/pr/src/pthreads/ptthread.c +++ b/mozilla/nsprpub/pr/src/pthreads/ptthread.c @@ -1643,7 +1643,6 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name) /* * On OSX, pthread_setname_np is only available in 10.6 or later, so test * for it at runtime. It also may not be available on all linux distros. - * The name length limit is 16 bytes. */ #if defined(DARWIN) int (*dynamic_pthread_setname_np)(const char*); @@ -1656,6 +1655,12 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name) if (!dynamic_pthread_setname_np) return PR_SUCCESS; + /* + * The 15-character name length limit is an experimentally determined + * length of a null-terminated string that most linux distros and OS X + * accept as an argument to pthread_setname_np. Otherwise the E2BIG + * error is returned by the function. + */ #define SETNAME_LENGTH_CONSTRAINT 15 #define SETNAME_FRAGMENT1_LENGTH (SETNAME_LENGTH_CONSTRAINT >> 1) #define SETNAME_FRAGMENT2_LENGTH \ @@ -1664,10 +1669,10 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThreadName(const char *name) if (nameLen > SETNAME_LENGTH_CONSTRAINT) { memcpy(name_dup, name, SETNAME_FRAGMENT1_LENGTH); name_dup[SETNAME_FRAGMENT1_LENGTH] = '~'; + /* Note that this also copies the null terminator. */ memcpy(name_dup + SETNAME_FRAGMENT1_LENGTH + 1, name + nameLen - SETNAME_FRAGMENT2_LENGTH, - SETNAME_FRAGMENT2_LENGTH); - name_dup[SETNAME_LENGTH_CONSTRAINT] = '\0'; + SETNAME_FRAGMENT2_LENGTH + 1); name = name_dup; }