Fix strict-aliasing warning in resolv/res_hconf.c

Message ID 5bfa6373-3817-4d31-a5ee-a8676e79b723@BAMAIL02.ba.imgtec.org
State Superseded
Headers

Commit Message

Steve Ellcey May 19, 2015, 9:09 p.m. UTC
  Here is a patch to clean up the strict-aliasing warning that we get
when compiling resolv/res_hconf.c with the latest top-of-tree GCC.
It uses the same casts as before but splits up the assignment into
two parts and that seems to be sufficient to get rid of the GCC
warning.

Is this OK to checkin?

Steve Ellcey
sellcey@imgtec.com


2015-05-19  Steve Ellcey  <sellcey@imgtec.com>

	* resolv/res_hconf.c (_res_hconf_reorder_addrs): Split up assignments
	to avoid GCC strict aliasing warning.
  

Comments

Paul Eggert May 19, 2015, 10:12 p.m. UTC | #1
On 05/19/2015 02:09 PM, Steve Ellcey wrote:
> It uses the same casts as before but splits up the assignment into
> two parts and that seems to be sufficient to get rid of the GCC
> warning.

It's warning about a portability problem that seems to be genuine. Can't 
we fix the problem using a union as before?  That should be better than 
trying to fool GCC into not warning about the problem.
  

Patch

diff --git a/resolv/res_hconf.c b/resolv/res_hconf.c
index 73942e8..c1c542a 100644
--- a/resolv/res_hconf.c
+++ b/resolv/res_hconf.c
@@ -407,6 +407,7 @@  _res_hconf_reorder_addrs (struct hostent *hp)
   if (num_ifs <= 0)
     {
       struct ifreq *ifr, *cur_ifr;
+      struct sockaddr_in *sin;
       int sd, num, i;
       /* Save errno.  */
       int save = errno;
@@ -443,14 +444,14 @@  _res_hconf_reorder_addrs (struct hostent *hp)
 		continue;
 
 	      ifaddrs[new_num_ifs].addrtype = AF_INET;
-	      ifaddrs[new_num_ifs].u.ipv4.addr =
-		((struct sockaddr_in *) &cur_ifr->ifr_addr)->sin_addr.s_addr;
+	      sin = (struct sockaddr_in *) &cur_ifr->ifr_addr;
+	      ifaddrs[new_num_ifs].u.ipv4.addr = sin->sin_addr.s_addr;
 
 	      if (__ioctl (sd, SIOCGIFNETMASK, cur_ifr) < 0)
 		continue;
 
-	      ifaddrs[new_num_ifs].u.ipv4.mask =
-		((struct sockaddr_in *) &cur_ifr->ifr_netmask)->sin_addr.s_addr;
+	      sin = (struct sockaddr_in *) &cur_ifr->ifr_netmask;
+	      ifaddrs[new_num_ifs].u.ipv4.mask = sin->sin_addr.s_addr;
 
 	      /* Now we're committed to this entry.  */
 	      ++new_num_ifs;