This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Fix strict-aliasing warning in resolv/res_hconf.c
- From: Steve Ellcey <sellcey at imgtec dot com>
- To: Paul Eggert <eggert at cs dot ucla dot edu>
- Cc: <libc-alpha at sourceware dot org>
- Date: Tue, 19 May 2015 15:53:09 -0700
- Subject: Re: [PATCH] Fix strict-aliasing warning in resolv/res_hconf.c
- Authentication-results: sourceware.org; auth=none
- References: <5bfa6373-3817-4d31-a5ee-a8676e79b723 at BAMAIL02 dot ba dot imgtec dot org> <555BB55E dot 3050304 at cs dot ucla dot edu>
- Reply-to: <sellcey at imgtec dot com>
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;