From fc7a68b5af12af235062a5f4c0c1a6af4ef9bb92 Mon Sep 17 00:00:00 2001 From: Joel Cunningham Date: Wed, 26 Apr 2017 08:29:44 -0500 Subject: [PATCH] sockets: fix CMSG alignment This changes the CMSG alignment macros to ensure struct cmsghdr and data are on a word (double word on 16-bit arch) aligned boundary We need to ensure at least 32-bit alignment for 16-bit systems because socklen_t could be 32-bit due to our definition --- src/include/lwip/sockets.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h index dcb3e877..8011cd51 100644 --- a/src/include/lwip/sockets.h +++ b/src/include/lwip/sockets.h @@ -144,9 +144,13 @@ struct cmsghdr { /* Data section follows header and possible padding, typically referred to as unsigned char cmsg_data[]; */ -/* cmsg header/data alignment */ -#define ALIGN_H(size) LWIP_MEM_ALIGN_SIZE(size) -#define ALIGN_D(size) LWIP_MEM_ALIGN_SIZE(size) +/* cmsg header/data alignment. NOTE: we align to native word size (double word +size on 16-bit arch) so structures are not placed at an unaligned address. +16-bit arch needs double word to ensure 32-bit alignment because socklen_t +could be 32 bits. If we ever have cmsg data with a 64-bit variable, alignment +will need to increase long long */ +#define ALIGN_H(size) (((size) + sizeof(long) - 1U) & ~(sizeof(long)-1U)) +#define ALIGN_D(size) ALIGN_H(size) #define CMSG_FIRSTHDR(mhdr) \ ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \