chiark / gitweb /
cleanup: provide helpful FILLZERO macro (for certain memset calls)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 13 Jul 2011 22:59:17 +0000 (23:59 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Aug 2011 22:03:28 +0000 (23:03 +0100)
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 <ijackson@chiark.greenend.org.uk>
secnet.h
transform.c
tun.c
udp.c

index 6559ef325874406a84267bda3ee9bdcdd4f49ad1..49afd322e83130506da37f96e3036e7ea57efed0 100644 (file)
--- 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 */
index b13c2dd9e88cfd3de573972a038ac7218df15fd0..8fdf9fd80a350b254bb4e2d7380a31a9babc77f9 100644 (file)
@@ -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 3db998f37fcd24d05b910d84111ea779fd9b4fbd..0baaf12226101cf83af5099cfac99e2fc6e464ae 100644 (file)
--- 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 7ff4d5fd18f84e5ade3487701484124c55767c9e..945d1d053749269a67d29d3cecfd8a2a1c8c302c 100644 (file)
--- 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) {