summaryrefslogtreecommitdiff
path: root/evthread_pthread.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2010-10-26 12:09:20 -0400
committerNick Mathewson <nickm@torproject.org>2010-10-26 12:09:20 -0400
commitfbaf0770a709aaf7ed50914e26ea3d5a350e189d (patch)
tree203eeff987e6492c07baf402f3fb404ed178fa01 /evthread_pthread.c
parentac1931ac3d3ed4977c95f3aedf047e7c3adedc7f (diff)
downloadlibevent-fbaf0770a709aaf7ed50914e26ea3d5a350e189d.tar.gz
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.
Diffstat (limited to 'evthread_pthread.c')
-rw-r--r--evthread_pthread.c10
1 files changed, 9 insertions, 1 deletions
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 <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 *