Bug 758837: add a comment to explain the source of the 15-char name length
authorwtc%google.com
Fri, 22 Jun 2012 01:36:02 +0000
changeset 4403 f65273acb78b1b1a705f2b4404e02b44fe3aa510
parent 4402 5c3a2654393f89f81312d8e78bbb93d4bc3aa477
child 4404 0e2b6336eca379acd4ba6fedc2e9a50e4c528713
push idunknown
push userunknown
push dateunknown
bugs758837
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.
pr/src/pthreads/ptthread.c
--- a/pr/src/pthreads/ptthread.c
+++ b/pr/src/pthreads/ptthread.c
@@ -1638,41 +1638,46 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThre
     memcpy(thread->name, name, nameLen + 1);
 
 #if defined(OPENBSD) || defined(FREEBSD)
     result = pthread_set_name_np(thread->id, name);
 #else /* not BSD */
     /*
      * 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*);
 #else
     int (*dynamic_pthread_setname_np)(pthread_t, const char*);
 #endif
 
     *(void**)(&dynamic_pthread_setname_np) =
         dlsym(RTLD_DEFAULT, "pthread_setname_np");
     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 \
     (SETNAME_LENGTH_CONSTRAINT - SETNAME_FRAGMENT1_LENGTH - 1)
     char name_dup[SETNAME_LENGTH_CONSTRAINT + 1];
     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;
     }
 
 #if defined(DARWIN)
     result = dynamic_pthread_setname_np(name);
 #else
     result = dynamic_pthread_setname_np(thread->id, name);
 #endif