From 72c00381999c613115c23bbf080a8a18c5a7e691 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 6 Mar 2010 09:53:40 +0100 Subject: [PATCH] Avoid dereferencing a type-punned pointer. When passing a file descriptor, we were dereferencing a unsigned char pointer. This breaks strict-aliasing rules in C99. Moreover, we should care about the alignment (even if in this case, this is aligned because the previous member of the struct is an int). Therefore, we use memcpy instead. --- src/privsep_fdpass.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/privsep_fdpass.c b/src/privsep_fdpass.c index 7235dfb8..4a1a3140 100644 --- a/src/privsep_fdpass.c +++ b/src/privsep_fdpass.c @@ -68,7 +68,7 @@ send_fd(int sock, int fd) cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - *(int *)CMSG_DATA(cmsg) = fd; + memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); } else { result = errno; } @@ -121,7 +121,7 @@ receive_fd(int sock) if (cmsg->cmsg_type != SCM_RIGHTS) LLOG_WARNX("expected type %d got %d", SCM_RIGHTS, cmsg->cmsg_type); - fd = (*(int *)CMSG_DATA(cmsg)); + memcpy(&fd, CMSG_DATA(cmsg), sizeof(int)); return fd; } else { errno = result; -- 2.39.5