[Pkg-telepathy-commits] [libnice] 59/265: tests: Fix strict aliasing of sockaddr structures

Simon McVittie smcv at debian.org
Wed May 14 12:04:53 UTC 2014


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch debian
in repository libnice.

commit fd6aa7b2617db328eee0ae42068d3a558e1a4f32
Author: Philip Withnall <philip.withnall at collabora.co.uk>
Date:   Wed Jan 15 08:51:28 2014 +0000

    tests: Fix strict aliasing of sockaddr structures
    
    Casting from one struct sockaddr type to another breaks C’s strict
    aliasing rules (variables of different types cannot alias). Fix this
    cleanly by using unions of struct sockaddrs to convert between the
    types (i.e. type-punning).
    
    I wish sockaddr didn’t have to be this painful.
    
    See:
    http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Optimize-Options.html#Type_002dpunning
---
 tests/test-address.c     |  9 ++++++---
 tests/test-new-dribble.c | 39 ++++++++++++++++++++++++---------------
 2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/tests/test-address.c b/tests/test-address.c
index 7f1608e..53378e0 100644
--- a/tests/test-address.c
+++ b/tests/test-address.c
@@ -96,7 +96,10 @@ test_ipv6 (void)
 {
   NiceAddress addr, other, v4addr;
   gchar str[NICE_ADDRESS_STRING_LEN];
-  struct sockaddr_in6 sin, sin2;
+  union {
+    struct sockaddr_in6 in6;
+    struct sockaddr addr;
+  } sin, sin2;
 
   g_assert (nice_address_set_from_string (&v4addr, "172.1.0.1") == TRUE);
 
@@ -121,8 +124,8 @@ test_ipv6 (void)
   nice_address_set_from_string (&other, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
   nice_address_set_port (&other, 9876); /* in native byte order */
 
-  nice_address_copy_to_sockaddr (&other, (struct sockaddr*)&sin2);
-  nice_address_copy_to_sockaddr (&addr, (struct sockaddr*)&sin);
+  nice_address_copy_to_sockaddr (&other, &sin2.addr);
+  nice_address_copy_to_sockaddr (&addr, &sin.addr);
   g_assert (nice_address_equal (&addr, &other) == TRUE);
   nice_address_to_string (&addr, str);
   nice_address_to_string (&other, str);
diff --git a/tests/test-new-dribble.c b/tests/test-new-dribble.c
index 9302455..f06e8bc 100644
--- a/tests/test-new-dribble.c
+++ b/tests/test-new-dribble.c
@@ -92,7 +92,10 @@ static const uint16_t known_attributes[] =  {
  */
 static int listen_socket (unsigned int port)
 {
-  struct sockaddr_in addr;
+  union {
+    struct sockaddr_in in;
+    struct sockaddr addr;
+  } addr;
   int fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
   if (fd == -1) {
@@ -101,11 +104,11 @@ static int listen_socket (unsigned int port)
   }
 
   memset (&addr, 0, sizeof (addr));
-  addr.sin_family = AF_INET;
-  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
-  addr.sin_port = htons(port);
+  addr.in.sin_family = AF_INET;
+  inet_pton(AF_INET, "127.0.0.1", &addr.in.sin_addr);
+  addr.in.sin_port = htons(port);
 
-  if (bind (fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in))) {
+  if (bind (fd, &addr.addr, sizeof (struct sockaddr_in))) {
     perror ("Error opening IP port");
     goto error;
   }
@@ -119,7 +122,10 @@ error:
 
 static int dgram_process (int sock, StunAgent *oldagent, StunAgent *newagent)
 {
-  struct sockaddr_storage addr;
+  union {
+    struct sockaddr_storage storage;
+    struct sockaddr addr;
+  } addr;
   socklen_t addr_len;
   uint8_t buf[STUN_MAX_MESSAGE_SIZE];
   size_t buf_len = 0;
@@ -134,7 +140,7 @@ static int dgram_process (int sock, StunAgent *oldagent, StunAgent *newagent)
 
 recv_packet:
   len = recvfrom (sock, buf, sizeof(buf), 0,
-      (struct sockaddr *)&addr, &addr_len);
+      &addr.addr, &addr_len);
 
   if (drop_stun_packets) {
     g_debug ("Dropping STUN packet as requested");
@@ -173,10 +179,10 @@ recv_packet:
       if (stun_message_has_cookie (&request))
         stun_message_append_xor_addr (&response,
             STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS,
-            (struct sockaddr *)&addr, addr_len);
+            &addr.addr, addr_len);
       else
          stun_message_append_addr (&response, STUN_ATTRIBUTE_MAPPED_ADDRESS,
-             (struct sockaddr *)&addr, addr_len);
+             &addr.addr, addr_len);
       break;
 
     default:
@@ -199,7 +205,7 @@ send_buf:
   }
   g_mutex_unlock (stun_mutex_ptr);
   len = sendto (sock, buf, buf_len, 0,
-      (struct sockaddr *)&addr, addr_len);
+      &addr.addr, addr_len);
   g_debug ("STUN response sent");
   drop_stun_packets = TRUE;
   ret = (len < buf_len) ? -1 : 0;
@@ -630,16 +636,19 @@ static void new_candidate_test(NiceAgent *lagent, NiceAgent *ragent)
 static void send_dummy_data(void)
 {
   int sockfd = listen_socket (4567);
-  struct sockaddr_in addr;
+  union {
+    struct sockaddr_in in;
+    struct sockaddr addr;
+  } addr;
 
   memset (&addr, 0, sizeof (addr));
-  addr.sin_family = AF_INET;
-  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
-  addr.sin_port = htons (IPPORT_STUN);
+  addr.in.sin_family = AF_INET;
+  inet_pton(AF_INET, "127.0.0.1", &addr.in.sin_addr);
+  addr.in.sin_port = htons (IPPORT_STUN);
 
   g_debug ("Sending dummy data to close STUN thread");
   sendto (sockfd, "close socket", 12, 0,
-          (struct sockaddr *)&addr, sizeof (addr));
+          &addr.addr, sizeof (addr));
 }
 
 int main(void)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-telepathy/libnice.git



More information about the Pkg-telepathy-commits mailing list