From a077adb4a0e9fdd8f96f5eee12a6613c134090f7 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Tue, 1 Nov 2016 13:00:05 +0000 Subject: [PATCH] Problem: pointer union for zmq_msg_t is a hack Solution: use compiler's alignment attributes instead which is clearer and less of a hack. Pointer alignment violations causing crashes on architectures such as sparc64 and aarch64. This also avoid triggering ABI checkers as the change is compatible even though applications that suffer from the bug should rebuild to take advantage of the fix. --- include/zmq.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/include/zmq.h b/include/zmq.h index d610d195..5d90ba0c 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -205,10 +205,21 @@ ZMQ_EXPORT int zmq_ctx_destroy (void *context); /* 0MQ message definition. */ /******************************************************************************/ -/* union here ensures correct alignment on architectures that require it, e.g. - * SPARC and ARM +/* Some architectures, like sparc64 and some variants of aarch64, enforce pointer + * alignment and raise sigbus on violations. Make sure applications allocate + * zmq_msg_t on addresses aligned on a pointer-size boundary to avoid this issue. */ -typedef union zmq_msg_t { unsigned char _ [64]; void *p; } zmq_msg_t; +typedef struct zmq_msg_t { +#if defined (__GNUC__) || defined ( __INTEL_COMPILER) || \ + (defined (__SUNPRO_C) && __SUNPRO_C >= 0x590) || \ + (defined (__SUNPRO_CC) && __SUNPRO_CC >= 0x590) + unsigned char _ [64] __attribute__ ((aligned (sizeof (void *)))); +#elif defined(_MSC_VER) + __declspec (align (sizeof (void *))) unsigned char _ [64]; +#else + unsigned char _ [64]; +#endif +} zmq_msg_t; typedef void (zmq_free_fn) (void *data, void *hint);