From fbaf0770a709aaf7ed50914e26ea3d5a350e189d Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 26 Oct 2010 12:09:20 -0400 Subject: [PATCH] Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof(long) 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 | 4 ++++ evthread_pthread.c | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/configure.in b/configure.in index ac60f9b0..c7936e5b 100644 --- a/configure.in +++ b/configure.in @@ -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 ] + ) fi AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no" && test "$enable_thread_support" != "no"]) diff --git a/evthread_pthread.c b/evthread_pthread.c index 59433737..d7439f7c 100644 --- a/evthread_pthread.c +++ b/evthread_pthread.c @@ -33,6 +33,7 @@ struct event_base; #include #include +#include #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 *