[Olsr-dev] [PATCH v1 08/14] parser: fix GCC 6 warnings

Ferry Huberts (spam-protected)
Wed Feb 24 00:46:17 CET 2016


From: Ferry Huberts <(spam-protected)>

src/parser.c:450:30: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
       void * src = &((struct sockaddr_in *)(void *)&from)->sin_addr;
                              ^~~~~~~~~~~
src/parser.c:454:30: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
       void * src = &((struct sockaddr_in6 *)(void *)&from)->sin6_addr;
                              ^~~~~~~~~~~~

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/parser.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/parser.c b/src/parser.c
index 0dd01d6..8295f36 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -445,14 +445,25 @@ olsr_input(int fd, void *data __attribute__ ((unused)), unsigned int flags __att
       }
       break;
     }
-    if (olsr_cnf->ip_version == AF_INET) {
-      /* IPv4 sender address */
-      void * src = &((struct sockaddr_in *)&from)->sin_addr;
-      memcpy(&from_addr.v4, src, sizeof(from_addr.v4));
-    } else {
-      /* IPv6 sender address */
-      void * src = &((struct sockaddr_in6 *)&from)->sin6_addr;
-      memcpy(&from_addr.v6, src, sizeof(from_addr.v6));
+
+    {
+      void * src;
+      void * dst;
+      size_t size;
+      if (olsr_cnf->ip_version == AF_INET) {
+        /* IPv4 sender address */
+        struct sockaddr_in * x = (struct sockaddr_in *) &from;
+        src = &x->sin_addr;
+        dst = &from_addr.v4;
+        size = sizeof(from_addr.v4);
+      } else {
+        /* IPv6 sender address */
+        struct sockaddr_in6 * x = (struct sockaddr_in6 *) &from;
+        src = &x->sin6_addr;
+        dst = &from_addr.v6;
+        size = sizeof(from_addr.v6);
+      }
+      memcpy(dst, src, size);
     }
 
 #ifdef DEBUG
-- 
2.5.0




More information about the Olsr-dev mailing list