# HG changeset patch # User Igor Sysoev # Date 1261495796 0 # Node ID 2c5e269d0608ab0fd9948a9708ef63ad2e0f2a53 # Parent 4e15548b29d40d1334cfa71b93ad5c25db54077f fix building by gcc 4.x with -O2/3/s in some Linux distributions: dereferencing type-punned pointer will break strict-aliasing rules diff --git a/src/os/unix/ngx_channel.c b/src/os/unix/ngx_channel.c --- a/src/os/unix/ngx_channel.c +++ b/src/os/unix/ngx_channel.c @@ -36,7 +36,18 @@ ngx_write_channel(ngx_socket_t s, ngx_ch cmsg.cm.cmsg_len = CMSG_LEN(sizeof(int)); cmsg.cm.cmsg_level = SOL_SOCKET; cmsg.cm.cmsg_type = SCM_RIGHTS; - *(int *) CMSG_DATA(&cmsg.cm) = ch->fd; + + /* + * We have to use ngx_memcpy() instead of simple + * *(int *) CMSG_DATA(&cmsg.cm) = ch->fd; + * because some gcc 4.4 with -O2/3/s optimization issues the warning: + * dereferencing type-punned pointer will break strict-aliasing rules + * + * Fortunately, gcc with -O1 compiles this ngx_memcpy() + * in the same simple assigment as in the code above + */ + + ngx_memcpy(CMSG_DATA(&cmsg.cm), &ch->fd, sizeof(int)); } msg.msg_flags = 0; @@ -153,7 +164,9 @@ ngx_read_channel(ngx_socket_t s, ngx_cha return NGX_ERROR; } - ch->fd = *(int *) CMSG_DATA(&cmsg.cm); + /* ch->fd = *(int *) CMSG_DATA(&cmsg.cm); */ + + ngx_memcpy(&ch->fd, CMSG_DATA(&cmsg.cm), sizeof(int)); } if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {