]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
Avoid dereferencing a type-punned pointer.
authorVincent <bernat@luffy.cx>
Sat, 6 Mar 2010 08:53:40 +0000 (09:53 +0100)
committerVincent <bernat@luffy.cx>
Sat, 6 Mar 2010 09:24:24 +0000 (10:24 +0100)
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

index 7235dfb85363dedca6549dc530da1c6af4177737..4a1a3140741cac77fb2e9798dfd2fecbf1024b29 100644 (file)
@@ -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;