2522c3
From 6e8efe88a07008a9f7f14de5b7f79dca2ba14602 Mon Sep 17 00:00:00 2001
2522c3
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
2522c3
Date: Sat, 30 Jan 2016 23:29:00 +0900
2522c3
Subject: [PATCH] async_netdb: kill gcc6 strict aliasing warning
2522c3
2522c3
POSIX.1-2008 sys/socket.h says "When a pointer to a
2522c3
sockaddr_storage structure is cast as a pointer to
2522c3
a protocol-specific address structure, ...",
2522c3
but now gcc 6 warns about breaking strict aliasing
2522c3
rules like:
2522c3
2522c3
./../utils/async_netdb.c: In function 'async_name_from_addr_finish':
2522c3
../../utils/async_netdb.c:239:36: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
2522c3
         raw_addr = &((const struct sockaddr_in *)&self->addr)->sin_addr;
2522c3
                                    ^~~~~~~~~~~
2522c3
../../utils/async_netdb.c:244:36: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
2522c3
         raw_addr = &((const struct sockaddr_in6 *)&self->addr)->sin6_addr;
2522c3
---
2522c3
 utils/async_netdb.c | 12 ++++++------
2522c3
 utils/async_netdb.h | 16 ++++++++++++++--
2522c3
 2 files changed, 20 insertions(+), 8 deletions(-)
2522c3
2522c3
diff --git a/utils/async_netdb.c b/utils/async_netdb.c
2522c3
index 5c52161..930da40 100644
2522c3
--- a/utils/async_netdb.c
2522c3
+++ b/utils/async_netdb.c
2522c3
@@ -31,8 +31,8 @@
2522c3
 
2522c3
 #if ASYNC_NETDB_USE_GAI
2522c3
 
2522c3
-# define _get_addr_family(addr) ((addr)->ss_family)
2522c3
-# define _get_addr_len(addr) ((addr)->ss_len)
2522c3
+# define _get_addr_family(addr) ((addr)->x_sockaddr_storage.ss_family)
2522c3
+# define _get_addr_len(addr) ((addr)->x_sockaddr_storage.ss_len)
2522c3
 
2522c3
 static int _has_threads;
2522c3
 
2522c3
@@ -45,8 +45,8 @@ int _async_netdb_is_done (struct io_thread *io)
2522c3
 
2522c3
 #else /* ASYNC_NETDB_USE_GAI */
2522c3
 
2522c3
-# define _get_addr_family(addr) ((addr)->sin_family)
2522c3
-# define _get_addr_len(addr) ((addr)->sin_len)
2522c3
+# define _get_addr_family(addr) ((addr)->x_sockaddr_in.sin_family)
2522c3
+# define _get_addr_len(addr) ((addr)->x_sockaddr_in.sin_len)
2522c3
 
2522c3
 static const int _has_threads = -1;
2522c3
 
2522c3
@@ -236,12 +236,12 @@ async_name_from_addr_finish (async_name_from_addr_t self_raw,
2522c3
     switch (_get_addr_family (&self->addr))
2522c3
       {
2522c3
       case AF_INET:
2522c3
-        raw_addr = &((const struct sockaddr_in *)&self->addr)->sin_addr;
2522c3
+        raw_addr = &self->addr.x_sockaddr_in.sin_addr;
2522c3
         addrlen = 4;
2522c3
         break;
2522c3
 #if ASYNC_NETDB_USE_GAI
2522c3
       case AF_INET6:
2522c3
-        raw_addr = &((const struct sockaddr_in6 *)&self->addr)->sin6_addr;
2522c3
+        raw_addr = &self->addr.x_sockaddr_in6.sin6_addr;
2522c3
         addrlen = 16;
2522c3
         break;
2522c3
 #endif /* ASYNC_NETDB_USE_GAI */
2522c3
diff --git a/utils/async_netdb.h b/utils/async_netdb.h
2522c3
index df6c59c..3d3101f 100644
2522c3
--- a/utils/async_netdb.h
2522c3
+++ b/utils/async_netdb.h
2522c3
@@ -53,13 +53,25 @@
2522c3
 
2522c3
 #if ASYNC_NETDB_USE_GAI
2522c3
 
2522c3
-typedef struct sockaddr_storage async_netdb_sockaddr_storage_t;
2522c3
+/* Without using union, gcc-6 warns for
2522c3
+   breaking strict aliasing rules
2522c3
+ */
2522c3
+typedef union {
2522c3
+	struct sockaddr_storage x_sockaddr_storage;
2522c3
+	struct sockaddr_in x_sockaddr_in;
2522c3
+	struct sockaddr_in6 x_sockaddr_in6;
2522c3
+} async_netdb_sockaddr_storage_t;
2522c3
 
2522c3
 int _async_netdb_is_done (struct io_thread *io);
2522c3
 
2522c3
 #else
2522c3
 
2522c3
-typedef struct sockaddr_in async_netdb_sockaddr_storage_t;
2522c3
+/* Because the definition for the above case is now union,
2522c3
+   the definition for this case must also be union...
2522c3
+*/
2522c3
+typedef union {
e8ff70
+	struct sockaddr_in x_sockaddr_in;
2522c3
+} async_netdb_sockaddr_storage_t;
2522c3
 
2522c3
 # ifndef EAI_SYSTEM
2522c3
 /* The EAI_* codes are specified specifically as preprocessor macros, so
2522c3
-- 
2522c3
2.7.0
2522c3