[CRIU] [PATCH 4/5] util-net: Fix strict aliasing problem on certain compilers

Cyrill Gorcunov gorcunov at openvz.org
Sat Jul 28 08:13:32 EDT 2012


gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
complains on strict aliasing problem if the program
get built with -O2 flag.

Make a workaround here.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 util-net.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/util-net.c b/util-net.c
index 090a8d7..435f43c 100644
--- a/util-net.c
+++ b/util-net.c
@@ -23,7 +23,7 @@ static void scm_fdset_init_chunk(struct scm_fdset *fdset, int nr_fds)
 static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr,
 		int saddr_len, bool with_flags)
 {
-	struct cmsghdr *cmsg;
+	struct cmsghdr *cmsg, from;
 
 	BUILD_BUG_ON(CR_SCM_MAX_FD > SCM_MAX_FD);
 	BUILD_BUG_ON(sizeof(fdset->msg_buf) < (CMSG_SPACE(sizeof(int) * CR_SCM_MAX_FD)));
@@ -39,10 +39,17 @@ static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr,
 	fdset->hdr.msg_control		= &fdset->msg_buf;
 	fdset->hdr.msg_controllen	= CMSG_LEN(sizeof(int) * CR_SCM_MAX_FD);
 
+	/*
+	 * GCC-4.4.5 has been complaining on strict aliasing
+	 * violation if we dereference cmsg directly, so lets
+	 * make a workaround and use memcpy here.
+	 */
 	cmsg				= CMSG_FIRSTHDR(&fdset->hdr);
-	cmsg->cmsg_len			= fdset->hdr.msg_controllen;
-	cmsg->cmsg_level		= SOL_SOCKET;
-	cmsg->cmsg_type			= SCM_RIGHTS;
+	from.cmsg_len			= fdset->hdr.msg_controllen;
+	from.cmsg_level			= SOL_SOCKET;
+	from.cmsg_type			= SCM_RIGHTS;
+
+	builtin_memcpy(cmsg, &from, sizeof(from));
 
 	return (int *)CMSG_DATA(cmsg);
 }
-- 
1.7.7.6



More information about the CRIU mailing list