moonbridge
changeset 18:2bdea79e5860
Use memcpy() instead of dereferencing type-punned pointers to avoid breaking strict-aliasing rules
author | jbe |
---|---|
date | Thu Jan 29 21:42:22 2015 +0100 (2015-01-29) |
parents | 78694b82078f |
children | be880e7c1e56 |
files | moonbridge.c |
line diff
1.1 --- a/moonbridge.c Thu Jan 29 21:29:15 2015 +0100 1.2 +++ b/moonbridge.c Thu Jan 29 21:42:22 2015 +0100 1.3 @@ -677,7 +677,7 @@ 1.4 control_message->cmsg_level = SOL_SOCKET; 1.5 control_message->cmsg_type = SCM_RIGHTS; 1.6 control_message->cmsg_len = CMSG_LEN(sizeof(int)); 1.7 - *((int *)CMSG_DATA(control_message)) = fd; 1.8 + memcpy(CMSG_DATA(control_message), &fd, sizeof(int)); 1.9 } 1.10 } 1.11 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) { 1.12 @@ -699,7 +699,7 @@ 1.13 if (moonbr_debug) { 1.14 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid); 1.15 } 1.16 - *((intptr_t *)buf) = (intptr_t)ptr; 1.17 + memcpy(buf, &ptr, sizeof(void *)); 1.18 while (len) { 1.19 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL); 1.20 if (written > 0) { 1.21 @@ -773,7 +773,7 @@ 1.22 if (control_message->cmsg_type != SCM_RIGHTS) { 1.23 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS"); 1.24 } 1.25 - *fd = *((int *)CMSG_DATA(control_message)); 1.26 + memcpy(fd, CMSG_DATA(control_message), sizeof(int)); 1.27 } else { 1.28 *fd = -1; 1.29 } 1.30 @@ -797,7 +797,11 @@ 1.31 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process"); 1.32 } 1.33 } 1.34 - return (void *)*(intptr_t *)buf; 1.35 + { 1.36 + void *ptr; /* avoid breaking strict-aliasing rules */ 1.37 + memcpy(&ptr, buf, sizeof(void *)); 1.38 + return ptr; 1.39 + } 1.40 } 1.41 1.42 /* Throws a Lua error message with an error string for errno appended to it */