]> granicus.if.org Git - libevent/commitdiff
Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof(long)
authorNick Mathewson <nickm@torproject.org>
Tue, 26 Oct 2010 16:09:20 +0000 (12:09 -0400)
committerNick Mathewson <nickm@torproject.org>
Tue, 26 Oct 2010 16:09:20 +0000 (12:09 -0400)
When pthread_t was smaller, our calculated thread IDs would include
uninitialized RAM, and so our unit tests would fail because thread_ids
would never match one another.

When pthread_t was larger and alignment was big-endian, our calculated
thread IDs would only have the most significant bytes of the
pthread_t, when in practice all the entropy is in the low-order bytes.

Found with help from Dagobert Michelsen.

configure.in
evthread_pthread.c

index ac60f9b0d6fa99098aedbfabeb95ae1553034d77..c7936e5b063e71df797efad47ff7832ca606472b 100644 (file)
@@ -529,6 +529,10 @@ if test x$bwin32 != xtrue && test "$enable_thread_support" != "no"; then
                [Define if we have pthreads on this system])
        have_pthreads=yes])
   CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+  AC_CHECK_SIZEOF(pthread_t, ,
+     [AC_INCLUDES_DEFAULT()
+      #include <pthread.h> ]
+  )
 fi
 AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no" && test "$enable_thread_support" != "no"])
 
index 594337370f2ac9a0ffe347fc2092b0841cda06c9..d7439f7c4ec94c3c668d57efe594cb75f3e7150b 100644 (file)
@@ -33,6 +33,7 @@ struct event_base;
 #include <event2/thread.h>
 
 #include <stdlib.h>
+#include <string.h>
 #include "mm-internal.h"
 #include "evthread-internal.h"
 
@@ -84,10 +85,17 @@ evthread_posix_get_id(void)
 {
        union {
                pthread_t thr;
+#if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
+               ev_uint64_t id;
+#else
                unsigned long id;
+#endif
        } r;
+#if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
+       memset(&r, 0, sizeof(r));
+#endif
        r.thr = pthread_self();
-       return r.id;
+       return (unsigned long)r.id;
 }
 
 static void *