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.
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;
}
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;