From 82d5fddfe78de48929babdcab268ea4a29ba7cf1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20Verg=C3=A9?= <adrienverge@gmail.com>
Date: Sun, 14 Feb 2016 19:01:24 +0100
Subject: [PATCH] ipv4: Fix new GCC 6 strict-aliasing errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The new version of the GCC compiler complains about an inline cast:

    In file included from /usr/include/string.h:630:0,
                     from src/ipv4.c:22:
    src/ipv4.c: In function ‘ipv4_show_route’:
    src/ipv4.h:62:12: error: dereferencing type-punned pointer will
    break strict-aliasing rules [-Werror=strict-aliasing]
      (((struct sockaddr_in *) &(route)->rt_dst)->sin_addr)
                ^
    src/ipv4.c:42:39: note: in expansion of macro ‘route_dest’
      strncat(show_route_buffer, inet_ntoa(route_dest(route)), 15);
                                           ^~~~~~~~~~

This commit introduces a dummy function to get rid of these errors.
---
 src/ipv4.c |  6 +++---
 src/ipv4.h | 17 +++++++++--------
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/ipv4.c b/src/ipv4.c
index 52c58b0..5485a4a 100644
--- a/src/ipv4.c
+++ b/src/ipv4.c
@@ -63,9 +63,9 @@ static inline int route_init(struct rtentry *route)
 		return ERR_IPV4_NO_MEM;
 	route_iface(route)[0] = '\0';
 
-	((struct sockaddr_in *) &(route)->rt_dst)->sin_family = AF_INET;
-	((struct sockaddr_in *) &(route)->rt_genmask)->sin_family = AF_INET;
-	((struct sockaddr_in *) &(route)->rt_gateway)->sin_family = AF_INET;
+	cast_addr(&(route)->rt_dst)->sin_family = AF_INET;
+	cast_addr(&(route)->rt_genmask)->sin_family = AF_INET;
+	cast_addr(&(route)->rt_gateway)->sin_family = AF_INET;
 
 	return 0;
 }
diff --git a/src/ipv4.h b/src/ipv4.h
index 42854ab..c797c23 100644
--- a/src/ipv4.h
+++ b/src/ipv4.h
@@ -58,14 +58,15 @@ struct ipv4_config {
 	struct rtentry	split_rt[MAX_SPLIT_ROUTES]; // split VPN routes
 };
 
-#define route_dest(route) \
-	(((struct sockaddr_in *) &(route)->rt_dst)->sin_addr)
-#define route_mask(route) \
-	(((struct sockaddr_in *) &(route)->rt_genmask)->sin_addr)
-#define route_gtw(route) \
-	(((struct sockaddr_in *) &(route)->rt_gateway)->sin_addr)
-#define route_iface(route) \
-	((route)->rt_dev)
+// Dummy function to make gcc 6 happy
+static inline struct sockaddr_in *cast_addr(struct sockaddr *addr)
+{
+	return (struct sockaddr_in *) addr;
+}
+#define route_dest(route)  (cast_addr(&(route)->rt_dst)->sin_addr)
+#define route_mask(route)  (cast_addr(&(route)->rt_genmask)->sin_addr)
+#define route_gtw(route)   (cast_addr(&(route)->rt_gateway)->sin_addr)
+#define route_iface(route) ((route)->rt_dev)
 
 struct tunnel;
 
-- 
GitLab