Sun Jan 29 18:33:07 2012 UTC ()
Allow using CMSG_NXTHDR with -Wcast-align.

If various checks are omitted, the CMSG_NXTHDR macro expands to
(struct cmsghdr *)((char *)(cmsg) + \
_ALIGN(((struct cmsghdr *)(cmsg))->cmsg_len))

Although there is no alignment problem (assuming cmsg is properly aligned
and _ALIGN is correct), this violates -Wcast-align on strict-alignment
architectures. Therefore an intermediate cast to void * is appropriate here.

There is no workaround other than not using -Wcast-align.

Taken from FreeBSD commit r220742 by jilles


(roy)
diff -r1.105 -r1.106 src/sys/sys/socket.h

cvs diff -r1.105 -r1.106 src/sys/sys/socket.h (expand / switch to unified diff)

--- src/sys/sys/socket.h 2012/01/25 00:28:35 1.105
+++ src/sys/sys/socket.h 2012/01/29 18:33:07 1.106
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: socket.h,v 1.105 2012/01/25 00:28:35 christos Exp $ */ 1/* $NetBSD: socket.h,v 1.106 2012/01/29 18:33:07 roy Exp $ */
2 2
3/* 3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -534,27 +534,27 @@ struct cmsghdr { @@ -534,27 +534,27 @@ struct cmsghdr {
534 * changes in ALIGNBYTES. 534 * changes in ALIGNBYTES.
535 */ 535 */
536#define __CMSG_ALIGN(n) (((n) + __ALIGNBYTES) & ~__ALIGNBYTES) 536#define __CMSG_ALIGN(n) (((n) + __ALIGNBYTES) & ~__ALIGNBYTES)
537#ifdef _KERNEL 537#ifdef _KERNEL
538#define CMSG_ALIGN(n) __CMSG_ALIGN(n) 538#define CMSG_ALIGN(n) __CMSG_ALIGN(n)
539#endif 539#endif
540 540
541/* given pointer to struct cmsghdr, return pointer to next cmsghdr */ 541/* given pointer to struct cmsghdr, return pointer to next cmsghdr */
542#define CMSG_NXTHDR(mhdr, cmsg) \ 542#define CMSG_NXTHDR(mhdr, cmsg) \
543 (((char *)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len) + \ 543 (((char *)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len) + \
544 __CMSG_ALIGN(sizeof(struct cmsghdr)) > \ 544 __CMSG_ALIGN(sizeof(struct cmsghdr)) > \
545 (((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen)) ? \ 545 (((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen)) ? \
546 (struct cmsghdr *)0 : \ 546 (struct cmsghdr *)0 : \
547 (struct cmsghdr *)((char *)(cmsg) + \ 547 (struct cmsghdr *)(void *)((char *)(cmsg) + \
548 __CMSG_ALIGN((cmsg)->cmsg_len))) 548 __CMSG_ALIGN((cmsg)->cmsg_len)))
549 549
550/* 550/*
551 * RFC 2292 requires to check msg_controllen, in case that the kernel returns 551 * RFC 2292 requires to check msg_controllen, in case that the kernel returns
552 * an empty list for some reasons. 552 * an empty list for some reasons.
553 */ 553 */
554#define CMSG_FIRSTHDR(mhdr) \ 554#define CMSG_FIRSTHDR(mhdr) \
555 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ 555 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
556 (struct cmsghdr *)(mhdr)->msg_control : \ 556 (struct cmsghdr *)(mhdr)->msg_control : \
557 (struct cmsghdr *)0) 557 (struct cmsghdr *)0)
558 558
559#define CMSG_SPACE(l) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(l)) 559#define CMSG_SPACE(l) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(l))
560#define CMSG_LEN(l) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + (l)) 560#define CMSG_LEN(l) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + (l))