From: Ralph Corderoy Date: Sun, 16 Oct 2016 17:49:32 +0000 (+0100) Subject: Add macro NEW0(p) that callocs, and use it in simple calls. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/81c57383273bb818858b2d7e6fdb4bbdb5df70d7 Add macro NEW0(p) that callocs, and use it in simple calls. p is a pointer, the size of the contents of the pointer is allocated with calloc via mh_xcalloc. It saves having to check every calloc to ensure the space requested matches the pointer to which it is assigned. --- diff --git a/h/utils.h b/h/utils.h index 4ae2f247..72191439 100644 --- a/h/utils.h +++ b/h/utils.h @@ -15,6 +15,9 @@ void *mh_xcalloc(size_t nelem, size_t elsize); /* Call free(3), if ptr isn't NULL. */ void mh_xfree(void *ptr); +/* Set p to point to newly allocated, zeroed, memory. */ +#define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p))) + char *pwd(void); char *add(const char *, char *); char *addlist(char *, const char *); diff --git a/sbr/addrsbr.c b/sbr/addrsbr.c index 56099574..70ddb8db 100644 --- a/sbr/addrsbr.c +++ b/sbr/addrsbr.c @@ -145,7 +145,7 @@ getm (char *str, char *dfhost, int dftype, char *eresult, size_t eresultsize) dftype = LOCALHOST; } - mp = mh_xcalloc(1, sizeof *mp); + NEW0(mp); mp->m_next = NULL; mp->m_text = getcpy (str); if (pers) diff --git a/sbr/datetime.c b/sbr/datetime.c index 4fb7bac0..8478414e 100644 --- a/sbr/datetime.c +++ b/sbr/datetime.c @@ -217,7 +217,7 @@ load_timezones (const contentline *clines) { ! strcasecmp ("VTIMEZONE", node->value)) { in_vtimezone = 1; - timezone = mh_xcalloc (1, sizeof (struct tzdesc)); + NEW0(timezone); if (timezones) { tzdesc_t t; diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index 84ef5983..5fd7fc96 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -289,7 +289,7 @@ static struct colormap colortable[] = { /* Add new component to the hash table */ #define NEWCOMP(cm,name) do { \ - cm = ((struct comp *) mh_xcalloc (1, sizeof (struct comp)));\ + NEW0(cm);\ cm->c_name = getcpy(name);\ cm->c_refcount++;\ ncomp++;\ @@ -564,7 +564,7 @@ do_name(char *sp, int preprocess) if (cm->c_tws) { memset (cm->c_tws, 0, sizeof *cm->c_tws); } else { - cm->c_tws = mh_xcalloc (1, sizeof *cm->c_tws); + NEW0(cm->c_tws); } fp->f_type = preprocess; PUTCOMP(sp); diff --git a/sbr/icalparse.y b/sbr/icalparse.y index f2e7b374..972ff983 100644 --- a/sbr/icalparse.y +++ b/sbr/icalparse.y @@ -146,8 +146,9 @@ remove_contentline (contentline *node) { contentline * add_contentline (contentline *node, const char *name) { - contentline *new_node = mh_xcalloc (1, sizeof (contentline)); + contentline *new_node; + NEW0(new_node); new_node->name = strdup (name); new_node->next = node->next; node->next = new_node; @@ -212,8 +213,9 @@ append (contentline *cline, const char *src, const size_t src_len) { static void new_input_line (contentline **cline) { - contentline *new_node = mh_xcalloc (1, sizeof (contentline)); + contentline *new_node; + NEW0(new_node); if (*cline) { /* Append the new node to the end of the list. */ (*cline)->last->next = new_node; @@ -228,7 +230,9 @@ new_input_line (contentline **cline) { static void new_vevent (vevent *event) { - vevent *new_node = mh_xcalloc (1, sizeof (vevent)), *node; + vevent *new_node, *node; + + NEW0(new_node); /* Append the new node to the end of the list. */ for (node = event; node->next; node = node->next) { continue; } @@ -237,9 +241,10 @@ new_vevent (vevent *event) { void add_param_name (contentline *cline, char *name) { - param_list *new_node = mh_xcalloc (1, sizeof (param_list)); + param_list *new_node; param_list *p; + NEW0(new_node); new_node->param_name = name; if (cline->params) { @@ -256,10 +261,11 @@ add_param_name (contentline *cline, char *name) { */ void add_param_value (contentline *cline, char *value) { - value_list *new_node = mh_xcalloc (1, sizeof (value_list)); + value_list *new_node; param_list *p; value_list *v; + NEW0(new_node); new_node->value = value; if (cline->params) { diff --git a/uip/ap.c b/uip/ap.c index aa56f7cb..92b765af 100644 --- a/uip/ap.c +++ b/uip/ap.c @@ -155,7 +155,7 @@ process (char *arg, int length) (q = &pq)->pq_next = NULL; while ((cp = getname (arg))) { - p = mh_xcalloc(1, sizeof *p); + NEW0(p); if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) { p->pq_text = getcpy (cp); p->pq_error = getcpy (error); diff --git a/uip/fmttest.c b/uip/fmttest.c index 40972b72..5d7e1930 100644 --- a/uip/fmttest.c +++ b/uip/fmttest.c @@ -420,7 +420,7 @@ process_addresses(struct format *fmt, struct msgs_array *addrs, for (i = 0; i < addrs->size; i++) { (q = &pq)->pq_next = NULL; while ((cp = getname(addrs->msgs[i]))) { - p = mh_xcalloc(1, sizeof *p); + NEW0(p); if ((mp = getm(cp, NULL, 0, error, sizeof(error))) == NULL) { p->pq_text = getcpy(cp); p->pq_error = getcpy(error); diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index 858bfe67..eb899d25 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -161,7 +161,7 @@ build_mime (char *infile, int autobuild, int dist, int directives, /* * Allocate space for primary (outside) content */ - ct = mh_xcalloc(1, sizeof *ct); + NEW0(ct); /* * Allocate structure for handling decoded content @@ -280,7 +280,7 @@ build_mime (char *infile, int autobuild, int dist, int directives, convert->filename = getcpy (filename); } } else { - convert = mh_xcalloc (sizeof *convert, 1); + NEW0(convert); convert->filename = getcpy (filename); convert->type = getcpy (type); @@ -320,7 +320,7 @@ build_mime (char *infile, int autobuild, int dist, int directives, convert->argstring = getcpy (argstring); } } else { - convert = mh_xcalloc (sizeof *convert, 1); + NEW0(convert); convert->type = getcpy (type); convert->argstring = getcpy (argstring); @@ -393,7 +393,7 @@ finish_field: ct->c_type = CT_MULTIPART; ct->c_subtype = MULTI_MIXED; - m = mh_xcalloc(1, sizeof *m); + NEW0(m); ct->c_ctparams = (void *) m; pp = &m->mp_parts; @@ -412,7 +412,7 @@ finish_field: if (!p) continue; - part = mh_xcalloc(1, sizeof *part); + NEW0(part); *pp = part; pp = &part->mp_next; part->mp_part = p; @@ -432,7 +432,7 @@ finish_field: adios("reading", "Unable to open %s for", at_entry->filename); } - p = mh_xcalloc(1, sizeof *p); + NEW0(p); init_decoded_content(p, infile); /* @@ -443,7 +443,7 @@ finish_field: setup_attach_content(p, at_entry->filename); - part = mh_xcalloc(1, sizeof *part); + NEW0(part); *pp = part; pp = &part->mp_next; part->mp_part = p; @@ -496,7 +496,7 @@ finish_field: struct part *part; struct text *t; - p = mh_xcalloc(1, sizeof *p); + NEW0(p); init_decoded_content(p, infile); if (get_ctinfo ("text/plain", p, 0) == NOTOK) @@ -514,11 +514,11 @@ finish_field: p->c_begin = ftell(in); p->c_end = ftell(in); - t = mh_xcalloc(1, sizeof *t); + NEW0(t); t->tx_charset = CHARSET_SPECIFIED; p->c_ctparams = t; - part = mh_xcalloc(1, sizeof *part); + NEW0(part); *pp = part; part->mp_part = p; } @@ -665,7 +665,7 @@ user_content (FILE *in, char *buf, CT *ctp, const char *infilename) } /* allocate basic Content structure */ - ct = mh_xcalloc(1, sizeof *ct); + NEW0(ct); *ctp = ct; /* allocate basic structure for handling decoded content */ @@ -896,7 +896,7 @@ use_forw: * reference, we need to create another Content structure * for the message/external-body to wrap it in. */ - ct = mh_xcalloc(1, sizeof *ct); + NEW0(ct); init_decoded_content(ct, infilename); *ctp = ct; if (get_ctinfo (buffer, ct, 0) == NOTOK) @@ -904,7 +904,7 @@ use_forw: ct->c_type = CT_MESSAGE; ct->c_subtype = MESSAGE_EXTERNAL; - e = mh_xcalloc(1, sizeof *e); + NEW0(e); ct->c_ctparams = (void *) e; e->eb_parent = ct; @@ -1013,7 +1013,7 @@ use_forw: ct->c_type = CT_MULTIPART; ct->c_subtype = MULTI_DIGEST; - m = mh_xcalloc(1, sizeof *m); + NEW0(m); ct->c_ctparams = (void *) m; pp = &m->mp_parts; @@ -1023,7 +1023,7 @@ use_forw: CT p; CE pe; - p = mh_xcalloc(1, sizeof *p); + NEW0(p); init_decoded_content (p, infilename); pe = &p->c_cefile; if (get_ctinfo ("message/rfc822", p, 0) == NOTOK) @@ -1036,7 +1036,7 @@ use_forw: if (listsw && stat (pe->ce_file, &st) != NOTOK) p->c_end = (long) st.st_size; - part = mh_xcalloc(1, sizeof *part); + NEW0(part); *pp = part; pp = &part->mp_next; part->mp_part = p; @@ -1096,7 +1096,7 @@ use_forw: ct->c_type = CT_MULTIPART; ct->c_subtype = vrsn; - m = mh_xcalloc(1, sizeof *m); + NEW0(m); ct->c_ctparams = (void *) m; pp = &m->mp_parts; @@ -1112,7 +1112,7 @@ use_forw: if (!p) continue; - part = mh_xcalloc(1, sizeof *part); + NEW0(part); *pp = part; pp = &part->mp_next; part->mp_part = p; @@ -2084,7 +2084,8 @@ set_charset (CT ct, int contains8bit) { struct text *t; if (ct->c_ctparams == NULL) { - t = ct->c_ctparams = mh_xcalloc(1, sizeof *t); + NEW0(t); + ct->c_ctparams = t; t->tx_charset = CHARSET_UNSPECIFIED; } else { t = (struct text *) ct->c_ctparams; @@ -2239,7 +2240,7 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, free (convert_command); /* Fill out the the new ct, reply_ct. */ - reply_ct = (CT) mh_xcalloc (1, sizeof *reply_ct); + NEW0(reply_ct); init_decoded_content (reply_ct, infile); if (extract_headers (reply_ct, reply_file, &reply_fp) == NOTOK) { @@ -2368,7 +2369,7 @@ expand_pseudoheader (CT ct, CT *text_plain_ct, struct multipart *m, reply_ct->c_cefile.ce_unlink = 1; /* Attach the new part to the parent multipart/mixed, "m". */ - part = (struct part *) mh_xcalloc (1, sizeof *part); + NEW0(part); part->mp_part = reply_ct; if (m->mp_parts) { struct part *p; diff --git a/uip/mhfixmsg.c b/uip/mhfixmsg.c index 4cccbad4..33faf468 100644 --- a/uip/mhfixmsg.c +++ b/uip/mhfixmsg.c @@ -1501,7 +1501,7 @@ static CT divide_part (CT ct) { CT new_part; - new_part = mh_xcalloc(1, sizeof *new_part); + NEW0(new_part); /* Just copy over what is needed for decoding. c_vrsn and c_celine aren't necessary. */ new_part->c_file = add (ct->c_file, NULL); @@ -1656,7 +1656,7 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { struct multipart *m; const struct str2init *ctinit; - ct = mh_xcalloc(1, sizeof *ct); + NEW0(ct); /* Set up the multipart/alternative part. These fields of *ct were initialized to 0 by mh_xcalloc(): @@ -1763,7 +1763,7 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { p->mp_next->mp_next = NULL; p->mp_next->mp_part = first_alt; - m = mh_xcalloc(1, sizeof *m); + NEW0(m); m->mp_start = concat (boundary, "\n", NULL); m->mp_stop = concat (boundary, "--\n", NULL); m->mp_parts = p; diff --git a/uip/mhlsbr.c b/uip/mhlsbr.c index 6cca7580..bd354a31 100644 --- a/uip/mhlsbr.c +++ b/uip/mhlsbr.c @@ -778,7 +778,7 @@ evalvar (struct mcomp *c1) return 1; } - args = (struct arglist *) mh_xcalloc ((size_t) 1, sizeof(struct arglist)); + NEW0(args); if (arglist_tail) arglist_tail->a_next = args; @@ -1188,7 +1188,7 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2) (q = &pq)->pq_next = NULL; while ((cp = getname (ap))) { - p = mh_xcalloc(1, sizeof *p); + NEW0(p); if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) { p->pq_text = getcpy (cp); p->pq_error = getcpy (error); @@ -1241,7 +1241,7 @@ add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int { struct mcomp *c1; - c1 = mh_xcalloc(1, sizeof *c1); + NEW0(c1); c1->c_flags = flags & ~INIT; if ((c1->c_name = name ? getcpy (name) : NULL)) c1->c_flags |= mcomp_flags (c1->c_name); diff --git a/uip/mhparse.c b/uip/mhparse.c index 7e539c3a..f43fd494 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -294,7 +294,7 @@ get_content (FILE *in, char *file, int toplevel) m_getfld_state_t gstate = 0; /* allocate the content structure */ - ct = mh_xcalloc(1, sizeof *ct); + NEW0(ct); ct->c_fp = in; ct->c_file = add (file, NULL); ct->c_begin = ftell (ct->c_fp) + 1; @@ -1027,7 +1027,7 @@ InitText (CT ct) ct->c_subtype = ct_str_subtype (CT_TEXT, ci->ci_subtype); /* allocate text character set structure */ - t = mh_xcalloc(1, sizeof *t); + NEW0(t); ct->c_ctparams = (void *) t; /* scan for charset parameter */ @@ -1131,7 +1131,7 @@ InitMultiPart (CT ct) } /* allocate primary structure for multipart info */ - m = mh_xcalloc(1, sizeof *m); + NEW0(m); ct->c_ctparams = (void *) m; /* check if boundary parameter contains only whitespace characters */ @@ -1175,7 +1175,7 @@ InitMultiPart (CT ct) if (strcmp (bufp + 2, m->mp_start)) continue; next_part: - part = mh_xcalloc(1, sizeof *part); + NEW0(part); *next = part; next = &part->mp_next; @@ -1414,7 +1414,7 @@ InitMessage (CT ct) PM pm; struct partial *p; - p = mh_xcalloc(1, sizeof *p); + NEW0(p); ct->c_ctparams = (void *) p; /* scan for parameters "id", "number", and "total" */ @@ -1462,7 +1462,7 @@ invalid_param: CT p; FILE *fp; - e = mh_xcalloc(1, sizeof *e); + NEW0(e); ct->c_ctparams = (void *) e; if (!ct->c_fp diff --git a/uip/picksbr.c b/uip/picksbr.c index 99d976d7..5ef4fc58 100644 --- a/uip/picksbr.c +++ b/uip/picksbr.c @@ -459,7 +459,7 @@ newnexus (int (*action)()) { register struct nexus *p; - p = mh_xcalloc(1, sizeof *p); + NEW0(p); p->n_action = action; return p; } diff --git a/uip/scansbr.c b/uip/scansbr.c index 804f72d5..c2ddf369 100644 --- a/uip/scansbr.c +++ b/uip/scansbr.c @@ -321,7 +321,7 @@ finished: if (datecomp) { if (! datecomp->c_text) { if (datecomp->c_tws == NULL) - datecomp->c_tws = mh_xcalloc(1, sizeof *datecomp->c_tws); + NEW0(datecomp->c_tws); *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime); datecomp->c_flags |= CF_DATEFAB|CF_TRUE; } else {