strict-aliasing error with gcc44 -O2
Maxim Dounin
mdounin at mdounin.ru
Sun Dec 20 12:37:08 MSK 2009
Hello!
On Fri, Dec 11, 2009 at 03:51:38PM +0300, Maxim Dounin wrote:
> Hello!
>
> I see the following error when compiling nginx 0.8.29 with gcc44
> with -O2:
>
> gcc44 -c -O -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -O2 -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/local/include -I objs -I src/http -I src/http/modules -o objs/src/http/ngx_http_core_module.o src/http/ngx_http_core_module.c
> cc1: warnings being treated as errors
> src/http/ngx_http_core_module.c: In function 'ngx_http_core_merge_srv_conf':
> src/http/ngx_http_core_module.c:2859: error: dereferencing pointer 'sin' does break strict-aliasing rules
> src/http/ngx_http_core_module.c:2853: error: dereferencing pointer 'sin' does break strict-aliasing rules
> src/http/ngx_http_core_module.c:2857: error: dereferencing pointer 'sin' does break strict-aliasing rules
> src/http/ngx_http_core_module.c:2851: note: initialized from here
> *** Error code 1
>
> gcc44 is most recent one from FreeBSD ports:
>
> $ gcc44 --version
> gcc44 (GCC) 4.4.3 20091201 (prerelease)
> ...
>
> Other gcc versions I have compile it cleanly (4.2, 4.3 and 4.5),
> and quick look at the code in question suggests that it's gcc44
> error as aliasing happens with u_char data and should be allowed.
> I haven't yet checked it deeply though.
Ah, I was wrong here. It has storage type of u_char, but accessed
via non-char lvalue, so gcc complains rightfully. Attached patch
fixes it by using memcpy() instead of aliasing.
BTW, Igor, why you don't just use sockaddr_storage for this? Is
it still not available on some of platforms supported?
Maxim Dounin
-------------- next part --------------
# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1261301580 0
# Node ID 10d2ad761df06cf25cd29f5d39c8bf5df521a0ac
# Parent 72104cd120ece09b3004261b2a29ace3e0aab812
Fix aliasing warning in gcc 4.4.
diff -r 72104cd120ec -r 10d2ad761df0 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c Tue Dec 15 00:00:00 2009 +0300
+++ b/src/http/ngx_http_core_module.c Sun Dec 20 09:33:00 2009 +0000
@@ -2812,7 +2812,7 @@
ngx_http_core_srv_conf_t *prev = parent;
ngx_http_core_srv_conf_t *conf = child;
- struct sockaddr_in *sin;
+ struct sockaddr_in sin;
ngx_http_listen_opt_t lsopt;
ngx_http_server_name_t *sn;
@@ -2847,16 +2847,17 @@
if (!conf->listen) {
ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
-
- sin = (struct sockaddr_in *) &lsopt.sockaddr;
-
- sin->sin_family = AF_INET;
+ ngx_memzero(&sin, sizeof(struct sockaddr_in));
+
+ sin.sin_family = AF_INET;
#if (NGX_WIN32)
- sin->sin_port = htons(80);
-#else
- sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
-#endif
- sin->sin_addr.s_addr = INADDR_ANY;
+ sin.sin_port = htons(80);
+#else
+ sin.sin_port = htons((getuid() == 0) ? 80 : 8000);
+#endif
+ sin.sin_addr.s_addr = INADDR_ANY;
+
+ ngx_memcpy(&lsopt.sockaddr, &sin, sizeof(struct sockaddr_in));
lsopt.socklen = sizeof(struct sockaddr_in);
More information about the nginx-devel
mailing list