From: Ian Jackson Date: Wed, 13 Jul 2011 22:59:17 +0000 (+0100) Subject: cleanup: provide helpful FILLZERO macro (for certain memset calls) X-Git-Tag: v0.2.0~29 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=commitdiff_plain;h=076bb54e68477f883033bee696c9c5f801ece2f2;hp=45cfab8ca7db61ce4677e2e77e76b9266c57ab12 cleanup: provide helpful FILLZERO macro (for certain memset calls) This macro replaces these idioms: memset(&foo,0,sizeof(foo)); => FILLZERO(foo); memset(foo,0,sizeof(*foo)); => FILLZERO(*foo); This makes it impossible to accidentally get the wrong size. Use this macro in all such patterns in secnet, apart from two in site.c which are going to be removed soon anyway. No intentional functional change. Signed-off-by: Ian Jackson --- diff --git a/secnet.h b/secnet.h index 6559ef3..49afd32 100644 --- a/secnet.h +++ b/secnet.h @@ -498,4 +498,6 @@ extern void log_from_fd(int fd, cstring_t prefix, struct log_if *log); #define STRING2(x) #x #define STRING(x) STRING2(x) +#define FILLZERO(obj) (memset(&(obj),0,sizeof((obj)))) + #endif /* secnet_h */ diff --git a/transform.c b/transform.c index b13c2dd..8fdf9fd 100644 --- a/transform.c +++ b/transform.c @@ -72,8 +72,8 @@ static void transform_delkey(void *sst) { struct transform_inst *ti=sst; - memset(&ti->cryptkey,0,sizeof(ti->cryptkey)); - memset(&ti->mackey,0,sizeof(ti->mackey)); + FILLZERO(ti->cryptkey); + FILLZERO(ti->mackey); ti->keyed=False; } @@ -252,7 +252,7 @@ static void transform_destroy(void *sst) { struct transform_inst *st=sst; - memset(st,0,sizeof(*st)); /* Destroy key material */ + FILLZERO(*st); /* Destroy key material */ free(st); } diff --git a/tun.c b/tun.c index 3db998f..0baaf12 100644 --- a/tun.c +++ b/tun.c @@ -211,7 +211,7 @@ static bool_t tun_set_route(void *sst, struct netlink_client *routes) struct sockaddr_in *sa; int action; - memset(&rt,0,sizeof(rt)); + FILLZERO(rt); sa=(struct sockaddr_in *)&rt.rt_dst; sa->sin_family=AF_INET; sa->sin_addr.s_addr=htonl(nets->list[i].prefix); @@ -292,7 +292,7 @@ static void tun_phase_hook(void *sst, uint32_t newphase) fatal_perror("%s: can't open device file %s",st->nl.name, st->device_path); } - memset(&ifr,0,sizeof(ifr)); + FILLZERO(ifr); ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Just send/receive IP packets, no extra headers */ if (st->interface_name) @@ -379,7 +379,7 @@ static void tun_phase_hook(void *sst, uint32_t newphase) /* Interface address */ strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ); sa=(struct sockaddr_in *)&ifr.ifr_addr; - memset(sa,0,sizeof(*sa)); + FILLZERO(*sa); sa->sin_family=AF_INET; sa->sin_addr.s_addr=htonl(st->local_address); if (ioctl(fd,SIOCSIFADDR, &ifr)!=0) { @@ -389,7 +389,7 @@ static void tun_phase_hook(void *sst, uint32_t newphase) /* Netmask */ strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ); sa=(struct sockaddr_in *)&ifr.ifr_netmask; - memset(sa,0,sizeof(*sa)); + FILLZERO(*sa); sa->sin_family=AF_INET; sa->sin_addr.s_addr=htonl(0xffffffff); if (ioctl(fd,SIOCSIFNETMASK, &ifr)!=0) { @@ -399,7 +399,7 @@ static void tun_phase_hook(void *sst, uint32_t newphase) /* Destination address (point-to-point) */ strncpy(ifr.ifr_name,st->interface_name,IFNAMSIZ); sa=(struct sockaddr_in *)&ifr.ifr_dstaddr; - memset(sa,0,sizeof(*sa)); + FILLZERO(*sa); sa->sin_family=AF_INET; sa->sin_addr.s_addr=htonl(st->nl.secnet_address); if (ioctl(fd,SIOCSIFDSTADDR, &ifr)!=0) { diff --git a/udp.c b/udp.c index 7ff4d5f..945d1d0 100644 --- a/udp.c +++ b/udp.c @@ -194,7 +194,7 @@ static void udp_phase_hook(void *sst, uint32_t new_phase) st->loc.file,st->loc.line); } - memset(&addr, 0, sizeof(addr)); + FILLZERO(addr); addr.sin_family=AF_INET; addr.sin_addr.s_addr=htonl(st->addr); addr.sin_port=htons(st->port); @@ -279,7 +279,7 @@ static list_t *udp_apply(closure_t *self, struct cloc loc, dict_t *context, l=dict_lookup(d,"proxy"); if (l) { st->use_proxy=True; - memset(&st->proxy,0,sizeof(st->proxy)); + FILLZERO(st->proxy); st->proxy.sin_family=AF_INET; i=list_elem(l,0); if (!i || i->type!=t_string) {