Bug 758837: Fix an off-by-one error in the thread name reduction code in NSPR_4_9_2_BETA2
authorwtc%google.com
Wed, 20 Jun 2012 16:44:23 +0000
changeset 4402 5c3a2654393f89f81312d8e78bbb93d4bc3aa477
parent 4401 245bb09ea7f5cf4eb74e5b29250157479aa960e9
child 4403 f65273acb78b1b1a705f2b4404e02b44fe3aa510
push idunknown
push userunknown
push dateunknown
bugs758837
Bug 758837: Fix an off-by-one error in the thread name reduction code in PR_SetCurrentThreadName in ptthread.c by changing nameLen to be just strlen(name). Use "~" instead of ".." to elide the middle part of a long thread name. Patch by Honza Bambas <honzab.moz@firemni.cz>. r=wtc. Modified Files: ptthread.c pruthr.c
pr/src/pthreads/ptthread.c
pr/src/threads/combined/pruthr.c
--- a/pr/src/pthreads/ptthread.c
+++ b/pr/src/pthreads/ptthread.c
@@ -1626,21 +1626,21 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThre
         return PR_FAILURE;
     }
 
     thread = PR_GetCurrentThread();
     if (!thread)
         return PR_FAILURE;
 
     PR_Free(thread->name);
-    nameLen = strlen(name) + 1;
-    thread->name = (char *)PR_Malloc(nameLen);
+    nameLen = strlen(name);
+    thread->name = (char *)PR_Malloc(nameLen + 1);
     if (!thread->name)
         return PR_FAILURE;
-    memcpy(thread->name, name, nameLen);
+    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.
@@ -1654,23 +1654,22 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThre
     *(void**)(&dynamic_pthread_setname_np) =
         dlsym(RTLD_DEFAULT, "pthread_setname_np");
     if (!dynamic_pthread_setname_np)
         return PR_SUCCESS;
 
 #define SETNAME_LENGTH_CONSTRAINT 15
 #define SETNAME_FRAGMENT1_LENGTH (SETNAME_LENGTH_CONSTRAINT >> 1)
 #define SETNAME_FRAGMENT2_LENGTH \
-    (SETNAME_LENGTH_CONSTRAINT - SETNAME_FRAGMENT1_LENGTH - 2)
+    (SETNAME_LENGTH_CONSTRAINT - SETNAME_FRAGMENT1_LENGTH - 1)
     char name_dup[SETNAME_LENGTH_CONSTRAINT + 1];
-    if (nameLen > SETNAME_LENGTH_CONSTRAINT + 1) {
+    if (nameLen > SETNAME_LENGTH_CONSTRAINT) {
         memcpy(name_dup, name, SETNAME_FRAGMENT1_LENGTH);
-        name_dup[SETNAME_FRAGMENT1_LENGTH] = '.';
-        name_dup[SETNAME_FRAGMENT1_LENGTH + 1] = '.';
-        memcpy(name_dup + SETNAME_FRAGMENT1_LENGTH + 2,
+        name_dup[SETNAME_FRAGMENT1_LENGTH] = '~';
+        memcpy(name_dup + SETNAME_FRAGMENT1_LENGTH + 1,
                name + nameLen - SETNAME_FRAGMENT2_LENGTH,
                SETNAME_FRAGMENT2_LENGTH);
         name_dup[SETNAME_LENGTH_CONSTRAINT] = '\0';
         name = name_dup;
     }
 
 #if defined(DARWIN)
     result = dynamic_pthread_setname_np(name);
--- a/pr/src/threads/combined/pruthr.c
+++ b/pr/src/threads/combined/pruthr.c
@@ -1592,21 +1592,21 @@ PR_IMPLEMENT(PRStatus) PR_SetCurrentThre
         return PR_FAILURE;
     }
 
     thread = PR_GetCurrentThread();
     if (!thread)
         return PR_FAILURE;
 
     PR_Free(thread->name);
-    nameLen = strlen(name) + 1;
-    thread->name = (char *)PR_Malloc(nameLen);
+    nameLen = strlen(name);
+    thread->name = (char *)PR_Malloc(nameLen + 1);
     if (!thread->name)
         return PR_FAILURE;
-    memcpy(thread->name, name, nameLen);
+    memcpy(thread->name, name, nameLen + 1);
     _PR_MD_SET_CURRENT_THREAD_NAME(thread->name);
     return PR_SUCCESS;
 }
 
 PR_IMPLEMENT(const char *) PR_GetThreadName(const PRThread *thread)
 {
     if (!thread)
         return NULL;