[leafnode-list] Re: leafnode 1.11.8 release candidate #1

Matthias Andree matthias.andree at gmx.de
Mon Jun 7 00:17:36 CEST 2010


Am 05.06.2010, 19:40 Uhr, schrieb clemens fischer:

> Matthias Andree wrote:
>
>> - - Fix compiler warning "dereferencing type-punned pointer will break
>>  strict-aliasing rules"
>
> What was it?

GCC was trying to convey that I'd used a cast to reinterpret a structure  
as a different one, and dereferenced its members.

The patch I made is attached.

-- 
Matthias Andree
-------------- next part --------------
commit c61b5986ce3270ec399d9cbc8266b160f1befc2b
Author: Matthias Andree <matthias.andree at gmx.de>
Date:   Fri Jan 22 21:23:42 2010 +0100

    Fix warning: dereferencing type-punned pointer will break strict-aliasing rules

diff --git a/NEWS b/NEWS
index 1df63bd..946acd1 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@
 - Performance optimization, reported by Gary R. Schmidt.
 - Fix a compiler warning on computers where "long" is a wider type than "int",
   such as many 64-bit computers.
+- Fix compiler warning "dereferencing type-punned pointer will break
+  strict-aliasing rules"
 
 ### CHANGES
 - Leafnode now enables IPv6 support by default. Use --without-ipv6 to disable.
diff --git a/checkpeerlocal.c b/checkpeerlocal.c
index 9893011..3cbf0b8 100644
--- a/checkpeerlocal.c
+++ b/checkpeerlocal.c
@@ -86,7 +86,12 @@ static void pat(struct sockaddr *addr)
  */
 int checkpeerlocal(int sock)
 {
-    char addr[128]; /* RATS: ignore */
+    union {
+	struct sockaddr_storage ss;
+	struct sockaddr_in sin;
+	struct sockaddr_in6 sin6;
+	struct sockaddr sa;
+    } addr;
     mastr *buf;
     struct ifconf ifc;
     struct ifreq *ifr, *end;
@@ -98,9 +103,9 @@ int checkpeerlocal(int sock)
     size = sizeof(addr);
 #ifdef TEST_LOCAL
     sock = socket(AF_INET, SOCK_DGRAM, 0);
-    if (getsockname(sock, (struct sockaddr *)addr, &size)) {
+    if (getsockname(sock, &addr.sa, &size)) {
 #else
-    if (getpeername(sock, (struct sockaddr *)addr, &size)) {
+    if (getpeername(sock, &addr.sa, &size)) {
 #endif
 	if (errno == ENOTSOCK)
 	    return 1;
@@ -108,7 +113,7 @@ int checkpeerlocal(int sock)
 	    return -1;
     }
 
-    type = ((struct sockaddr *)addr)->sa_family;
+    type = addr.ss.ss_family;
     D(printf("address type of peer socket: %d\n", type));
 
     switch(type) {
@@ -123,11 +128,11 @@ int checkpeerlocal(int sock)
 	    return -2;
     }
 
-    D(pat((struct sockaddr *)addr));
+    D(pat(&addr.sa));
 
 #ifdef HAVE_IPV6
     if (type == AF_INET6) {
-	struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)addr;
+	struct sockaddr_in6 *i6 = &addr.sin6;
 	D((printf("IPv6 address\n")));
 	/* IPv6 localhost */
 	if (IN6_IS_ADDR_LOOPBACK(&i6->sin6_addr)) {
@@ -146,14 +151,14 @@ int checkpeerlocal(int sock)
 	    si.sin_family = AF_INET;
 	    si.sin_port = i6->sin6_port;
 	    memcpy(&si.sin_addr, &(i6->sin6_addr.s6_addr[12]), 4);
-	    memcpy(addr, &si, sizeof(struct sockaddr_in));
+	    memcpy(&addr.sin, &si, sizeof(struct sockaddr_in));
 	} else {
 	    return 0;
 	}
     }
 #endif
 
-    D(pat((struct sockaddr *)addr));
+    D(pat(&addr.sa));
 
     buf = mastr_new(2048);
 
@@ -265,7 +270,7 @@ int checkpeerlocal(int sock)
 		D(printf("address/netmask: %s/", inet_ntoa(adr)));
 		D(printf("%s\n", inet_ntoa(msk)));
 
-		if ((((struct sockaddr_in *)addr)->sin_addr.s_addr & msk.s_addr) 
+		if ((addr.sin.sin_addr.s_addr & msk.s_addr)
 			== (adr.s_addr & msk.s_addr)) {
 		    D(printf("found\n"));
 		    close(newsock);


More information about the leafnode-list mailing list