[pisa-src] r1213 - trunk/test/ifaddr.c

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Fri, 16 Oct 2009 11:38:02 +0200

Author: tjansen
Date: Fri Oct 16 11:38:02 2009
New Revision: 1213

Log:
Fixed a warning:

ifaddr.c: In function ‘print_ifaddr’:
ifaddr.c:80: warning: dereferencing pointer ‘addr’ does break strict-aliasing 
rules
ifaddr.c:78: note: initialized from here

Note that the memcpy reduces performance. Since it is a test application and
the function is not critical for performance, using this workaround seems to
be acceptable.

Modified:
   trunk/test/ifaddr.c

Modified: trunk/test/ifaddr.c
==============================================================================
--- trunk/test/ifaddr.c Fri Oct 16 11:30:30 2009        (r1212)
+++ trunk/test/ifaddr.c Fri Oct 16 11:38:02 2009        (r1213)
@@ -75,11 +75,10 @@
        strcpy(ifr.ifr_name, name);
        ifr.ifr_addr.sa_family = AF_INET;
        if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
-               struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
-
-               printf("addr = %s\n", (char *)inet_ntoa(addr->sin_addr));
-       }
-       else {
+               struct sockaddr_in addr;
+               memcpy(&addr, &ifr.ifr_addr, sizeof(addr));
+               printf("addr = %s\n", (char *)inet_ntoa(addr.sin_addr));
+       } else {
                fprintf(stderr, "ERROR: %s\n", strerror(errno));
        }
 

Other related posts: