# HG changeset patch # User jbe # Date 1422564142 -3600 # Node ID 2bdea79e5860434d6c26bd98c3a6149fff2bd8e3 # Parent 78694b82078fe8a349e830e3873b1049b919c173 Use memcpy() instead of dereferencing type-punned pointers to avoid breaking strict-aliasing rules diff -r 78694b82078f -r 2bdea79e5860 moonbridge.c --- a/moonbridge.c Thu Jan 29 21:29:15 2015 +0100 +++ b/moonbridge.c Thu Jan 29 21:42:22 2015 +0100 @@ -677,7 +677,7 @@ control_message->cmsg_level = SOL_SOCKET; control_message->cmsg_type = SCM_RIGHTS; control_message->cmsg_len = CMSG_LEN(sizeof(int)); - *((int *)CMSG_DATA(control_message)) = fd; + memcpy(CMSG_DATA(control_message), &fd, sizeof(int)); } } while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) { @@ -699,7 +699,7 @@ if (moonbr_debug) { moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid); } - *((intptr_t *)buf) = (intptr_t)ptr; + memcpy(buf, &ptr, sizeof(void *)); while (len) { written = send(worker->controlfd, pos, len, MSG_NOSIGNAL); if (written > 0) { @@ -773,7 +773,7 @@ if (control_message->cmsg_type != SCM_RIGHTS) { moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS"); } - *fd = *((int *)CMSG_DATA(control_message)); + memcpy(fd, CMSG_DATA(control_message), sizeof(int)); } else { *fd = -1; } @@ -797,7 +797,11 @@ moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process"); } } - return (void *)*(intptr_t *)buf; + { + void *ptr; /* avoid breaking strict-aliasing rules */ + memcpy(&ptr, buf, sizeof(void *)); + return ptr; + } } /* Throws a Lua error message with an error string for errno appended to it */