Fix strict-aliasing warning in resolv/res_hconf.c
Commit Message
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
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.
@@ -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;