Mike Waychison
2010-Mar-22 18:50 UTC
[klibc] [PATCH] ipconfig: Use memcpy to avoid strict aliasing warnings.
Newer compilers will complain about strict aliasing when casting struct sockaddr to sockaddr_in. Even though these are legitimate type casts, changing them to use memcpy is easy enough and silences the warnings. Signed-off-by: Mike Waychison <mikew at google.com> --- usr/kinit/ipconfig/netdev.c | 27 +++++++++++++++++---------- 1 files changed, 17 insertions(+), 10 deletions(-) diff --git a/usr/kinit/ipconfig/netdev.c b/usr/kinit/ipconfig/netdev.c index e0f7dad..6e3014f 100644 --- a/usr/kinit/ipconfig/netdev.c +++ b/usr/kinit/ipconfig/netdev.c @@ -41,10 +41,12 @@ int netdev_getflags(struct netdev *dev, short *flags) static int netdev_sif_addr(struct ifreq *ifr, int cmd, uint32_t addr) { - struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr; + struct sockaddr_in sin; + + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = addr; - sin->sin_family = AF_INET; - sin->sin_addr.s_addr = addr; + memcpy(&ifr->ifr_addr, &sin, sizeof sin); return ioctl(cfd, cmd, ifr); } @@ -76,6 +78,15 @@ int netdev_setaddress(struct netdev *dev) return 0; } +static void set_s_addr(struct sockaddr *saddr, uint32_t ipaddr) +{ + struct sockaddr_in sin = { + .sin_family = AF_INET, + .sin_addr.s_addr = ipaddr, + }; + memcpy(saddr, &sin, sizeof sin); +} + int netdev_setdefaultroute(struct netdev *dev) { struct rtentry r; @@ -85,13 +96,9 @@ int netdev_setdefaultroute(struct netdev *dev) memset(&r, 0, sizeof(r)); - ((struct sockaddr_in *)&r.rt_dst)->sin_family = AF_INET; - ((struct sockaddr_in *)&r.rt_dst)->sin_addr.s_addr = INADDR_ANY; - ((struct sockaddr_in *)&r.rt_gateway)->sin_family = AF_INET; - ((struct sockaddr_in *)&r.rt_gateway)->sin_addr.s_addr - dev->ip_gateway; - ((struct sockaddr_in *)&r.rt_genmask)->sin_family = AF_INET; - ((struct sockaddr_in *)&r.rt_genmask)->sin_addr.s_addr = INADDR_ANY; + set_s_addr(&r.rt_dst, INADDR_ANY); + set_s_addr(&r.rt_gateway, dev->ip_gateway); + set_s_addr(&r.rt_genmask, INADDR_ANY); r.rt_flags = RTF_UP | RTF_GATEWAY; if (ioctl(cfd, SIOCADDRT, &r) == -1 && errno != EEXIST) { -- 1.7.0.1
maximilian attems
2010-Mar-22 19:52 UTC
[klibc] [PATCH] ipconfig: Use memcpy to avoid strict aliasing warnings.
On Mon, 22 Mar 2010, Mike Waychison wrote:> Newer compilers will complain about strict aliasing when casting struct > sockaddr to sockaddr_in. Even though these are legitimate type casts, changing > them to use memcpy is easy enough and silences the warnings. >thanks applied to patch queue and pulled out.