From 35ef9d158396dbf013a862dd8f830db64909826b Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Mon, 21 Aug 2017 11:09:02 +0100 Subject: [PATCH] Add ZERO(p) for the typical memset(p, 0, sizeof *p) dance. Also seen as memset(&foo->bar_xyzzy, 0, sizeof foo->bar_xyzzy). I find it tedious to keep checking the parameters are in agreement when reading the code. --- h/utils.h | 3 +++ sbr/client.c | 2 +- sbr/datetime.c | 2 +- sbr/dtimep.l | 3 ++- sbr/fmt_compile.c | 2 +- sbr/fmt_scan.c | 2 +- sbr/mts.c | 2 +- sbr/netsec.c | 2 +- uip/fmttest.c | 2 +- uip/replsbr.c | 2 +- 10 files changed, 13 insertions(+), 9 deletions(-) diff --git a/h/utils.h b/h/utils.h index 19fbdcc6..dd3fb431 100644 --- a/h/utils.h +++ b/h/utils.h @@ -31,6 +31,9 @@ void mh_xfree(void *ptr); /* Set p to point to newly allocated, zeroed, memory. */ #define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p))) +/* Zero the bytes to which p points. */ +#define ZERO(p) memset((p), 0, sizeof *(p)) + char *pwd(void); char *add(const char *, char *); char *addlist(char *, const char *); diff --git a/sbr/client.c b/sbr/client.c index 1a7cb7fd..a64461bc 100644 --- a/sbr/client.c +++ b/sbr/client.c @@ -28,7 +28,7 @@ client (char *server, char *service, char *response, int len_response, return NOTOK; } - memset(&hints, 0, sizeof(hints)); + ZERO(&hints); #ifdef AI_ADDRCONFIG hints.ai_flags = AI_ADDRCONFIG; #endif diff --git a/sbr/datetime.c b/sbr/datetime.c index a5f6c77b..9ecff53c 100644 --- a/sbr/datetime.c +++ b/sbr/datetime.c @@ -64,7 +64,7 @@ parse_datetime (const char *datetime, const char *zone, int dst, int form_1 = 0; int items_matched; - memset(tws, 0, sizeof *tws); + ZERO(tws); items_matched = sscanf (datetime, "%4d%2d%2dT%2d%2d%2d%c", &tws->tw_year, &tws->tw_mon, &tws->tw_mday, diff --git a/sbr/dtimep.l b/sbr/dtimep.l index e9dd8153..238af863 100644 --- a/sbr/dtimep.l +++ b/sbr/dtimep.l @@ -11,6 +11,7 @@ %a5000 %{ #include +#include #include /* Since we're looking at a string at a time, don't worry about @@ -231,7 +232,7 @@ nl [ \t\n()] char *cp; static struct tws tw; - memset(&tw,0,sizeof(struct tws)); + ZERO(&tw); lexhandle = yy_scan_string(lexstr); %} diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index 5a4e4b8b..389e16c4 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -559,7 +559,7 @@ do_name(char *sp, int preprocess) CERROR("component used as both date and address"); } if (cm->c_tws) { - memset (cm->c_tws, 0, sizeof *cm->c_tws); + ZERO(cm->c_tws); } else { NEW0(cm->c_tws); } diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 075b7dd6..49fc717f 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -989,7 +989,7 @@ fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat, *comp->c_tws = *tws; comp->c_flags &= ~CF_TRUE; } else if ((comp->c_flags & CF_DATEFAB) == 0) { - memset (comp->c_tws, 0, sizeof *comp->c_tws); + ZERO(comp->c_tws); comp->c_flags = CF_TRUE; } comp->c_flags |= CF_PARSED; diff --git a/sbr/mts.c b/sbr/mts.c index ae617cec..f6180b03 100644 --- a/sbr/mts.c +++ b/sbr/mts.c @@ -235,7 +235,7 @@ LocalName (int flag) gethostname (buf, sizeof(buffer0) - 1); /* now fully qualify our name */ - memset(&hints, 0, sizeof(hints)); + ZERO(&hints); hints.ai_flags = AI_CANONNAME; hints.ai_family = PF_UNSPEC; if (getaddrinfo(buf, NULL, &hints, &res) == 0) { diff --git a/sbr/netsec.c b/sbr/netsec.c index 3fcb2cf5..58cab1bb 100644 --- a/sbr/netsec.c +++ b/sbr/netsec.c @@ -1217,7 +1217,7 @@ netsec_negotiate_sasl(netsec_context *nsc, const char *mechlist, char **errstr) * messages. */ - memset(&secprops, 0, sizeof(secprops)); + ZERO(&secprops); secprops.maxbufsize = SASL_MAXRECVBUF; /* diff --git a/uip/fmttest.c b/uip/fmttest.c index 83e5c26d..e5323e64 100644 --- a/uip/fmttest.c +++ b/uip/fmttest.c @@ -336,7 +336,7 @@ main (int argc, char **argv) */ if (dupaddrs == 0 || trace) { - memset(&cb, 0, sizeof(cb)); + ZERO(&cb); cbp = &cb; if (dupaddrs == 0) { diff --git a/uip/replsbr.c b/uip/replsbr.c index d3ba2709..2444aa3c 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -205,7 +205,7 @@ finished: dat[2] = 0; dat[3] = outputlinelen; dat[4] = 0; - memset(&cb, 0, sizeof(cb)); + ZERO(&cb); cb.formataddr = replformataddr; cb.concataddr = replconcataddr; fmt_scan (fmt, scanl, i, dat, &cb); -- 2.56.0