INN commit: trunk (6 files)

INN Commit rra at isc.org
Tue Jul 30 20:13:01 UTC 2013


    Date: Tuesday, July 30, 2013 @ 13:13:01
  Author: iulius
Revision: 9514

fix cast-align warnings

GCC on some platforms warns about casts from struct sockaddr to one
of the more specific subtypes on the grounds that the alignment
requirements increase.  Restructure code in getaddrinfo.c to avoid
needing the casts and cast through void * everywhere else to suppress
the warnings.  (In all cases, we are assured that the underlying data
is properly aligned.)

Thanks to Russ Allbery for the patch (from rra-c-util).

Modified:
  trunk/innd/rc.c
  trunk/lib/getaddrinfo.c
  trunk/lib/getnameinfo.c
  trunk/lib/network.c
  trunk/tests/lib/getaddrinfo-t.c
  trunk/tests/lib/network-t.c

---------------------------+
 innd/rc.c                 |   16 ++++++++--------
 lib/getaddrinfo.c         |   15 ++++++++-------
 lib/getnameinfo.c         |    2 +-
 lib/network.c             |   20 ++++++++++----------
 tests/lib/getaddrinfo-t.c |   28 ++++++++++++++--------------
 tests/lib/network-t.c     |    2 +-
 6 files changed, 42 insertions(+), 41 deletions(-)

Modified: innd/rc.c
===================================================================
--- innd/rc.c	2013-07-26 20:30:29 UTC (rev 9513)
+++ innd/rc.c	2013-07-30 20:13:01 UTC (rev 9514)
@@ -149,19 +149,19 @@
 #ifdef HAVE_INET6
     if (s_local->sa_family == AF_INET6)
     {
-	port1 = ntohs(((struct sockaddr_in6 *) s_local)->sin6_port);
-	port2 = ntohs(((struct sockaddr_in6 *) s_distant)->sin6_port);
-	((struct sockaddr_in6 *) s_local)->sin6_port = 0;
-	((struct sockaddr_in6 *) s_distant)->sin6_port = htons(PORT_IDENTD);
+        port1 = ntohs(((struct sockaddr_in6 *) (void *) s_local)->sin6_port);
+        port2 = ntohs(((struct sockaddr_in6 *) (void *) s_distant)->sin6_port);
+        ((struct sockaddr_in6 *) (void *) s_local)->sin6_port = 0;
+        ((struct sockaddr_in6 *) (void *) s_distant)->sin6_port = htons(PORT_IDENTD);
 	ident_fd = socket(PF_INET6, SOCK_STREAM, 0);
     } else
 #endif
     if (s_local->sa_family == AF_INET)
     {
-	port1 = ntohs(((struct sockaddr_in *) s_local)->sin_port);
-        port2 = ntohs(((struct sockaddr_in *) s_distant)->sin_port);
-	((struct sockaddr_in *) s_local)->sin_port = 0;
-	((struct sockaddr_in *) s_distant)->sin_port = htons(PORT_IDENTD);
+        port1 = ntohs(((struct sockaddr_in *) (void *) s_local)->sin_port);
+        port2 = ntohs(((struct sockaddr_in *) (void *) s_distant)->sin_port);
+        ((struct sockaddr_in *) (void *) s_local)->sin_port = 0;
+        ((struct sockaddr_in *) (void *) s_distant)->sin_port = htons(PORT_IDENTD);
 	ident_fd = socket(PF_INET, SOCK_STREAM, 0);
     } else
     {

Modified: lib/getaddrinfo.c
===================================================================
--- lib/getaddrinfo.c	2013-07-26 20:30:29 UTC (rev 9513)
+++ lib/getaddrinfo.c	2013-07-30 20:13:01 UTC (rev 9514)
@@ -176,15 +176,17 @@
                  unsigned short port)
 {
     struct addrinfo *ai;
+    struct sockaddr_in *sin;
 
     ai = malloc(sizeof(*ai));
     if (ai == NULL)
         return NULL;
-    ai->ai_addr = malloc(sizeof(struct sockaddr_in));
-    if (ai->ai_addr == NULL) {
+    sin = calloc(1, sizeof(struct sockaddr_in));
+    if (sin == NULL) {
         free(ai);
         return NULL;
     }
+    ai->ai_addr = (struct sockaddr *) sin;
     ai->ai_next = NULL;
     if (canonical == NULL)
         ai->ai_canonname = NULL;
@@ -195,16 +197,15 @@
             return NULL;
         }
     }
-    memset(ai->ai_addr, 0, sizeof(struct sockaddr_in));
     ai->ai_flags = 0;
     ai->ai_family = AF_INET;
     ai->ai_socktype = socktype;
     ai->ai_protocol = (socktype == SOCK_DGRAM) ? IPPROTO_UDP : IPPROTO_TCP;
     ai->ai_addrlen = sizeof(struct sockaddr_in);
-    ((struct sockaddr_in *) ai->ai_addr)->sin_family = AF_INET;
-    ((struct sockaddr_in *) ai->ai_addr)->sin_addr = addr;
-    ((struct sockaddr_in *) ai->ai_addr)->sin_port = htons(port);
-    sin_set_length((struct sockaddr_in *) ai->ai_addr);
+    sin->sin_family = AF_INET;
+    sin->sin_addr = addr;
+    sin->sin_port = htons(port);
+    sin_set_length(sin);
     return ai;
 }
 

Modified: lib/getnameinfo.c
===================================================================
--- lib/getnameinfo.c	2013-07-26 20:30:29 UTC (rev 9513)
+++ lib/getnameinfo.c	2013-07-30 20:13:01 UTC (rev 9514)
@@ -148,7 +148,7 @@
     /* We only support AF_INET. */
     if (sa->sa_family != AF_INET)
         return EAI_FAMILY;
-    sin = (const struct sockaddr_in *) sa;
+    sin = (const struct sockaddr_in *) (const void *) sa;
 
     /* Name lookup. */
     if (node != NULL && nodelen > 0) {

Modified: lib/network.c
===================================================================
--- lib/network.c	2013-07-26 20:30:29 UTC (rev 9513)
+++ lib/network.c	2013-07-30 20:13:01 UTC (rev 9514)
@@ -645,7 +645,7 @@
     if (addr->sa_family == AF_INET6) {
         const struct sockaddr_in6 *sin6;
 
-        sin6 = (const struct sockaddr_in6 *) addr;
+        sin6 = (const struct sockaddr_in6 *) (void *) addr;
         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
             struct in_addr in;
 
@@ -659,7 +659,7 @@
     if (addr->sa_family == AF_INET) {
         const struct sockaddr_in *sin;
 
-        sin = (const struct sockaddr_in *) addr;
+        sin = (const struct sockaddr_in *) (void *) addr;
         result = inet_ntop(AF_INET, &sin->sin_addr, dst, size);
         return (result != NULL);
     } else {
@@ -677,20 +677,20 @@
 bool
 network_sockaddr_equal(const struct sockaddr *a, const struct sockaddr *b)
 {
-    const struct sockaddr_in *a4 = (const struct sockaddr_in *) a;
-    const struct sockaddr_in *b4 = (const struct sockaddr_in *) b;
+    const struct sockaddr_in *a4 = (const struct sockaddr_in *) (void *) a;
+    const struct sockaddr_in *b4 = (const struct sockaddr_in *) (void *) b;
 
 #ifdef HAVE_INET6
-    const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) a;
-    const struct sockaddr_in6 *b6 = (const struct sockaddr_in6 *) b;
+    const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) (void *) a;
+    const struct sockaddr_in6 *b6 = (const struct sockaddr_in6 *) (void *) b;
     const struct sockaddr *tmp;
 
     if (a->sa_family == AF_INET && b->sa_family == AF_INET6) {
         tmp = a;
         a = b;
         b = tmp;
-        a6 = (const struct sockaddr_in6 *) a;
-        b4 = (const struct sockaddr_in *) b;
+        a6 = (const struct sockaddr_in6 *) (void *) a;
+        b4 = (const struct sockaddr_in *) (void *) b;
     }
     if (a->sa_family == AF_INET6) {
         if (b->sa_family == AF_INET6)
@@ -726,14 +726,14 @@
     const struct sockaddr_in6 *sin6;
 
     if (sa->sa_family == AF_INET6) {
-        sin6 = (const struct sockaddr_in6 *) sa;
+        sin6 = (const struct sockaddr_in6 *) (void *) sa;
         return htons(sin6->sin6_port);
     }
 #endif
     if (sa->sa_family != AF_INET)
         return 0;
     else {
-        sin = (const struct sockaddr_in *) sa;
+        sin = (const struct sockaddr_in *) (void *) sa;
         return htons(sin->sin_port);
     }
 }

Modified: tests/lib/getaddrinfo-t.c
===================================================================
--- tests/lib/getaddrinfo-t.c	2013-07-26 20:30:29 UTC (rev 9513)
+++ tests/lib/getaddrinfo-t.c	2013-07-30 20:13:01 UTC (rev 9514)
@@ -71,7 +71,7 @@
     is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol");
     is_string(NULL, ai->ai_canonname, "...no canonname");
     is_int(sizeof(struct sockaddr_in), ai->ai_addrlen, "...right addrlen");
-    saddr = (struct sockaddr_in *) ai->ai_addr;
+    saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
     is_int(htons(25), saddr->sin_port, "...right port");
     ok(saddr->sin_addr.s_addr == htonl(0x7f000001UL), "...right address");
     test_freeaddrinfo(ai);
@@ -81,7 +81,7 @@
     hints.ai_socktype = SOCK_STREAM;
     ok(test_getaddrinfo(NULL, "25", &hints, &ai) == 0, "passive lookup");
     is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
-    saddr = (struct sockaddr_in *) ai->ai_addr;
+    saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
     is_int(htons(25), saddr->sin_port, "...right port");
     ok(saddr->sin_addr.s_addr == INADDR_ANY, "...right address");
     test_freeaddrinfo(ai);
@@ -94,7 +94,7 @@
         ok(test_getaddrinfo(NULL, "smtp", &hints, &ai) == 0,
            "service of smtp");
         is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(25), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr == INADDR_ANY, "...right address");
         test_freeaddrinfo(ai);
@@ -109,7 +109,7 @@
        "AI_NUMERICSERV with 25 space");
     ok(test_getaddrinfo(NULL, "25", &hints, &ai) == 0,
        "valid AI_NUMERICSERV");
-    saddr = (struct sockaddr_in *) ai->ai_addr;
+    saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
     is_int(htons(25), saddr->sin_port, "...right port");
     ok(saddr->sin_addr.s_addr == htonl(0x7f000001UL), "...right address");
     test_freeaddrinfo(ai);
@@ -135,7 +135,7 @@
     is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol");
     is_string(NULL, ai->ai_canonname, "...no canonname");
     is_int(sizeof(struct sockaddr_in), ai->ai_addrlen, "...right addrlen");
-    saddr = (struct sockaddr_in *) ai->ai_addr;
+    saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
     is_int(0, saddr->sin_port, "...right port");
     ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
     test_freeaddrinfo(ai);
@@ -149,7 +149,7 @@
         is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
         is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol");
         is_string(NULL, ai->ai_canonname, "...no canonname");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(25), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
         test_freeaddrinfo(ai);
@@ -162,7 +162,7 @@
        "AI_NUMERICHOST and AI_NUMERICSERV with symbolic name");
     ok(test_getaddrinfo("10.20.30.40", "25", &hints, &ai) == 0,
        "valid AI_NUMERICHOST and AI_NUMERICSERV");
-    saddr = (struct sockaddr_in *) ai->ai_addr;
+    saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
     is_int(htons(25), saddr->sin_port, "...right port");
     ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
     test_freeaddrinfo(ai);
@@ -174,7 +174,7 @@
         ok(test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0,
            "AI_NUMERICHOST and AI_CANONNAME");
         is_string("10.20.30.40", ai->ai_canonname, "...right canonname");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(25), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
         test_freeaddrinfo(ai);
@@ -190,7 +190,7 @@
            "domain service with UDP hint");
         is_int(SOCK_DGRAM, ai->ai_socktype, "...right socktype");
         is_string(NULL, ai->ai_canonname, "...no canonname");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(53), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
         test_freeaddrinfo(ai);
@@ -207,7 +207,7 @@
            "lookup of www.isc.org");
         is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
         is_string(NULL, ai->ai_canonname, "...no canonname");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(80), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr != INADDR_ANY, "...address is something");
         addr = saddr->sin_addr;
@@ -217,7 +217,7 @@
         ok(test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0,
            "lookup of www.isc.org with A_CANONNAME");
         ok(ai->ai_canonname != NULL, "...canonname isn't null");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(80), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr == addr.s_addr, "...and same address");
         test_freeaddrinfo(ai);
@@ -230,7 +230,7 @@
     else {
         ok(test_getaddrinfo("cnn.com", "80", NULL, &ai) == 0,
            "lookup of cnn.com with multiple A records");
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         is_int(htons(80), saddr->sin_port, "...right port");
         ok(saddr->sin_addr.s_addr != INADDR_ANY, "...address is something");
         test_freeaddrinfo(ai);
@@ -253,7 +253,7 @@
     hints.ai_flags = AI_CANONNAME;
     ok(test_getaddrinfo("cnn.com", NULL, &hints, &ai) == 0,
        "lookup of cnn.com");
-    saddr = (struct sockaddr_in *) ai->ai_addr;
+    saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
     is_int(0, saddr->sin_port, "...port is 0");
     first = ai;
     for (found = 0; ai != NULL; ai = ai->ai_next) {
@@ -266,7 +266,7 @@
             break;
         }
         found = 0;
-        saddr = (struct sockaddr_in *) ai->ai_addr;
+        saddr = (struct sockaddr_in *) (void *) ai->ai_addr;
         addr = saddr->sin_addr;
         for (i = 0; host->h_addr_list[i] != NULL; i++)
             if (memcmp(&addr, host->h_addr_list[i], host->h_length) == 0)

Modified: tests/lib/network-t.c
===================================================================
--- tests/lib/network-t.c	2013-07-26 20:30:29 UTC (rev 9513)
+++ tests/lib/network-t.c	2013-07-30 20:13:01 UTC (rev 9514)
@@ -113,7 +113,7 @@
     listener_handler(client);
     is_int(AF_INET, saddr->sa_family, "...address family is IPv4");
     is_int(htonl(0x7f000001UL),
-           ((struct sockaddr_in *) saddr)->sin_addr.s_addr,
+           ((struct sockaddr_in *) (void *) saddr)->sin_addr.s_addr,
            "...and client address is 127.0.0.1");
     free(saddr);
     for (i = 0; i < count; i++)



More information about the inn-committers mailing list