[PATCH] Fix strict-aliasing warning in resolv/res_hconf.c
Steve Ellcey
sellcey@imgtec.com
Tue May 19 23:52:00 GMT 2015
On Tue, 2015-05-19 at 15:12 -0700, Paul Eggert wrote:
> 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.
OK, how is this version with no casts.
Steve Ellcey
sellcey@imgtec.com
2015-05-19 Steve Ellcey <sellcey@imgtec.com>
* resolv/res_hconf.c (_res_hconf_reorder_addrs): Split up assignment
and use union to avoid GCC strict aliasing warning.
diff --git a/resolv/res_hconf.c b/resolv/res_hconf.c
index 73942e8..0dbee2e 100644
--- a/resolv/res_hconf.c
+++ b/resolv/res_hconf.c
@@ -407,6 +407,11 @@ _res_hconf_reorder_addrs (struct hostent *hp)
if (num_ifs <= 0)
{
struct ifreq *ifr, *cur_ifr;
+ union
+ {
+ struct sockaddr *sa;
+ struct sockaddr_in *sin;
+ } ss;
int sd, num, i;
/* Save errno. */
int save = errno;
@@ -443,14 +448,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;
+ ss.sa = &cur_ifr->ifr_addr;
+ ifaddrs[new_num_ifs].u.ipv4.addr = ss.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;
+ ss.sa = &cur_ifr->ifr_netmask;
+ ifaddrs[new_num_ifs].u.ipv4.mask = ss.sin->sin_addr.s_addr;
/* Now we're committed to this entry. */
++new_num_ifs;
More information about the Libc-alpha
mailing list