From: Ralph Corderoy Date: Sun, 16 Oct 2016 18:38:36 +0000 (+0100) Subject: Add NEW(p) that sets p to mh_xmalloc'd memory sized by *p. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/2d5d9e243c91784909b11948894e3ba0989107c0?ds=sidebyside;hp=81c57383273bb818858b2d7e6fdb4bbdb5df70d7 Add NEW(p) that sets p to mh_xmalloc'd memory sized by *p. Use it for the simple cases. Again, saves having to check the same identifier is given, possibly far apart after a superfluous cast. --- diff --git a/h/utils.h b/h/utils.h index 72191439..c866a52c 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, uninitialised, memory. */ +#define NEW(p) ((p) = mh_xmalloc(sizeof *(p))) + /* Set p to point to newly allocated, zeroed, memory. */ #define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p))) diff --git a/sbr/charstring.c b/sbr/charstring.c index d94961aa..00c65986 100644 --- a/sbr/charstring.c +++ b/sbr/charstring.c @@ -42,8 +42,9 @@ charstring_reserve (charstring_t s, size_t need) { */ charstring_t charstring_create (size_t max) { - charstring_t s = mh_xmalloc (sizeof *s); + charstring_t s; + NEW(s); s->max = NMH_MAX_CHARWIDTH * (max ? max : CHARSTRING_DEFAULT_SIZE); s->cur = s->buffer = mh_xmalloc (s->max); s->chars = 0; @@ -54,8 +55,9 @@ charstring_create (size_t max) { charstring_t charstring_copy (const charstring_t src) { const size_t num = src->cur - src->buffer; - charstring_t s = mh_xmalloc (sizeof *s); + charstring_t s; + NEW(s); s->max = src->max; s->buffer = mh_xmalloc (s->max); memcpy (s->buffer, src->buffer, num); diff --git a/sbr/context_foil.c b/sbr/context_foil.c index 12dab45f..aabac558 100644 --- a/sbr/context_foil.c +++ b/sbr/context_foil.c @@ -44,9 +44,8 @@ context_foil (char *path) * If path is given, create a minimal profile/context list */ if (path) { - m_defs = (struct node *) mh_xmalloc (sizeof(*np)); - - np = m_defs; + NEW(np); + m_defs = np; if (!(np->n_name = strdup ("Path"))) { advise (NULL, "strdup failed"); return -1; diff --git a/sbr/context_replace.c b/sbr/context_replace.c index 45d5a31d..0e9e1146 100644 --- a/sbr/context_replace.c +++ b/sbr/context_replace.c @@ -20,9 +20,8 @@ context_replace (char *key, char *value) * If list is empty, allocate head of profile/context list. */ if (!m_defs) { - m_defs = (struct node *) mh_xmalloc (sizeof(*np)); - - np = m_defs; + NEW(np); + m_defs = np; np->n_name = getcpy (key); np->n_field = getcpy (value); np->n_context = 1; @@ -54,8 +53,7 @@ context_replace (char *key, char *value) /* * Else add this new entry at the end */ - np->n_next = (struct node *) mh_xmalloc (sizeof(*np)); - + NEW(np->n_next); np = np->n_next; np->n_name = getcpy (key); np->n_field = getcpy (value); diff --git a/sbr/crawl_folders.c b/sbr/crawl_folders.c index d8bcdba3..004e8cf2 100644 --- a/sbr/crawl_folders.c +++ b/sbr/crawl_folders.c @@ -135,7 +135,8 @@ crawl_folders_body (struct crawl_context *crawl, void crawl_folders (char *dir, crawl_callback_t *callback, void *baton) { - struct crawl_context *crawl = mh_xmalloc (sizeof(*crawl)); + struct crawl_context *crawl; + NEW(crawl); crawl->max = CRAWL_NUMFOLDERS; crawl->total = crawl->start = crawl->foldp = 0; crawl->folders = mh_xmalloc (crawl->max * sizeof(*crawl->folders)); diff --git a/sbr/folder_read.c b/sbr/folder_read.c index 73ffe431..e4bac904 100644 --- a/sbr/folder_read.c +++ b/sbr/folder_read.c @@ -39,8 +39,7 @@ folder_read (char *name, int lockflag) } /* Allocate the main structure for folder information */ - mp = (struct msgs *) mh_xmalloc ((size_t) sizeof(*mp)); - + NEW(mp); clear_folder_flags (mp); mp->foldpath = name; mp->lowmsg = 0; diff --git a/sbr/lock_file.c b/sbr/lock_file.c index a8a98118..71a559c6 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -660,7 +660,7 @@ timerON (char *curlock, int fd) struct lock *lp; size_t len; - lp = (struct lock *) mh_xmalloc (sizeof(*lp)); + NEW(lp); len = strlen(curlock) + 1; lp->l_fd = fd; diff --git a/sbr/m_getfld.c b/sbr/m_getfld.c index 135be1db..0fe55499 100644 --- a/sbr/m_getfld.c +++ b/sbr/m_getfld.c @@ -268,7 +268,8 @@ void m_getfld_state_init (m_getfld_state_t *gstate, FILE *iob) { m_getfld_state_t s; - s = *gstate = (m_getfld_state_t) mh_xmalloc(sizeof (struct m_getfld_state)); + NEW(s); + *gstate = s; s->readpos = s->end = s->msg_buf; s->bytes_read = s->total_bytes_read = 0; s->last_caller_pos = s->last_internal_pos = 0; diff --git a/sbr/netsec.c b/sbr/netsec.c index eb28cbac..c31e7295 100644 --- a/sbr/netsec.c +++ b/sbr/netsec.c @@ -137,8 +137,9 @@ static int checkascii(const unsigned char *byte, size_t len); netsec_context * netsec_init(void) { - netsec_context *nsc = mh_xmalloc(sizeof(*nsc)); + netsec_context *nsc; + NEW(nsc); nsc->ns_readfd = -1; nsc->ns_writefd = -1; nsc->ns_snoop = 0; @@ -1052,7 +1053,7 @@ int netsec_get_user(void *context, int id, const char **result, */ if (nsc->sasl_creds == NULL) { - nsc->sasl_creds = mh_xmalloc(sizeof(*nsc->sasl_creds)); + NEW(nsc->sasl_creds); nsc->sasl_creds->user = NULL; nsc->sasl_creds->password = NULL; } @@ -1092,7 +1093,7 @@ netsec_get_password(sasl_conn_t *conn, void *context, int id, return SASL_BADPARAM; if (nsc->sasl_creds == NULL) { - nsc->sasl_creds = mh_xmalloc(sizeof(*nsc->sasl_creds)); + NEW(nsc->sasl_creds); nsc->sasl_creds->user = NULL; nsc->sasl_creds->password = NULL; } diff --git a/sbr/oauth.c b/sbr/oauth.c index 0cc95e4e..22e69874 100755 --- a/sbr/oauth.c +++ b/sbr/oauth.c @@ -255,8 +255,10 @@ make_user_agent() boolean mh_oauth_new(mh_oauth_ctx **result, const char *svc_name) { - mh_oauth_ctx *ctx = *result = mh_xmalloc(sizeof *ctx); + mh_oauth_ctx *ctx; + NEW(ctx); + *result = ctx; ctx->curl = NULL; ctx->log = NULL; @@ -507,7 +509,7 @@ mh_oauth_authorize(const char *code, mh_oauth_ctx *ctx) return NULL; } - result = mh_xmalloc(sizeof *result); + NEW(result); result->ctx = ctx; result->access_token = result->refresh_token = NULL; @@ -622,7 +624,8 @@ load_creds(struct user_creds **result, FILE *fp, mh_oauth_ctx *ctx) int state; m_getfld_state_t getfld_ctx = 0; - struct user_creds *user_creds = mh_xmalloc(sizeof *user_creds); + struct user_creds *user_creds; + NEW(user_creds); user_creds->alloc = 4; user_creds->len = 0; user_creds->creds = mh_xmalloc(user_creds->alloc * sizeof *user_creds->creds); diff --git a/sbr/readconfig.c b/sbr/readconfig.c index 5d116893..70984022 100644 --- a/sbr/readconfig.c +++ b/sbr/readconfig.c @@ -61,7 +61,7 @@ readconfig (struct node **npp, FILE *ib, const char *file, int ctx) switch (state = m_getfld (&gstate, name, field, &fieldsz, ib)) { case FLD: case FLDPLUS: - np = (struct node *) mh_xmalloc (sizeof(*np)); + NEW(np); *npp = np; *(npp = &np->n_next) = NULL; np->n_name = getcpy (name); @@ -157,11 +157,12 @@ readconfig (struct node **npp, FILE *ib, const char *file, int ctx) void add_profile_entry (const char *key, const char *value) { - struct node *newnode = (struct node *) mh_xmalloc (sizeof *newnode); + struct node *newnode; /* This inserts the new node at the beginning of m_defs because that doesn't require traversing it or checking to see if it's empty. */ + NEW(newnode); newnode->n_name = getcpy (key); newnode->n_field = getcpy (value); newnode->n_context = 0; diff --git a/sbr/vector.c b/sbr/vector.c index 10058d4d..d243f353 100644 --- a/sbr/vector.c +++ b/sbr/vector.c @@ -46,8 +46,11 @@ static void bvector_resize (bvector_t, size_t); bvector_t bvector_create (size_t init_size) { - bvector_t vec = mh_xmalloc (sizeof *vec); - size_t bytes = BVEC_BYTES (vec, init_size ? init_size : VEC_INIT_SIZE); + bvector_t vec; + size_t bytes; + + NEW(vec); + bytes = BVEC_BYTES (vec, init_size ? init_size : VEC_INIT_SIZE); vec->bits = mh_xmalloc (bytes); memset (vec->bits, 0, bytes); @@ -144,9 +147,10 @@ static void svector_resize (svector_t, size_t); svector_t svector_create (size_t init_size) { - svector_t vec = mh_xmalloc (sizeof *vec); + svector_t vec; size_t bytes; + NEW(vec); vec->maxsize = init_size ? init_size : VEC_INIT_SIZE; bytes = vec->maxsize * sizeof (char *); vec->strs = mh_xmalloc (bytes); @@ -223,9 +227,10 @@ static void ivector_resize (ivector_t, size_t); ivector_t ivector_create (size_t init_size) { - ivector_t vec = mh_xmalloc (sizeof *vec); + ivector_t vec; size_t bytes; + NEW(vec); vec->maxsize = init_size ? init_size : VEC_INIT_SIZE; bytes = vec->maxsize * sizeof (int); vec->ints = mh_xmalloc (bytes); diff --git a/uip/aliasbr.c b/uip/aliasbr.c index 0f6b1909..65cd5e44 100644 --- a/uip/aliasbr.c +++ b/uip/aliasbr.c @@ -478,7 +478,7 @@ add_aka (struct aka *ak, char *pp) if (!strcmp (pp, ad->ad_text)) return; - ad = (struct adr *) mh_xmalloc (sizeof(*ad)); + NEW(ad); ad->ad_text = getcpy (pp); ad->ad_local = strchr(pp, '@') == NULL && strchr(pp, '!') == NULL; ad->ad_next = NULL; @@ -519,8 +519,7 @@ akalloc (char *id) { register struct aka *p; - p = (struct aka *) mh_xmalloc (sizeof(*p)); - + NEW(p); p->ak_name = getcpy (id); p->ak_visible = 0; p->ak_addr = NULL; @@ -540,8 +539,7 @@ hmalloc (struct passwd *pw) { register struct home *p; - p = (struct home *) mh_xmalloc (sizeof(*p)); - + NEW(p); p->h_name = getcpy (pw->pw_name); p->h_uid = pw->pw_uid; p->h_gid = pw->pw_gid; diff --git a/uip/fmttest.c b/uip/fmttest.c index 5d7e1930..d6026b95 100644 --- a/uip/fmttest.c +++ b/uip/fmttest.c @@ -351,7 +351,7 @@ main (int argc, char **argv) if (trace) { struct trace_context *ctx; - ctx = mh_xmalloc(sizeof(*ctx)); + NEW(ctx); ctx->num = -1; ctx->str = dummy; ctx->outbuf = getcpy(NULL); diff --git a/uip/install-mh.c b/uip/install-mh.c index 6b8f4442..a79793f5 100644 --- a/uip/install-mh.c +++ b/uip/install-mh.c @@ -180,8 +180,8 @@ query: /* * Add some initial elements to the profile/context list */ - m_defs = (struct node *) mh_xmalloc (sizeof *np); - np = m_defs; + NEW(np); + m_defs = np; np->n_name = getcpy ("Path"); np->n_field = getcpy (pathname); np->n_context = 0; diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index eb899d25..e867824f 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -243,7 +243,7 @@ build_mime (char *infile, int autobuild, int dist, int directives, goto finish_field; } - entry = mh_xmalloc(sizeof(*entry)); + NEW(entry); entry->filename = getcpy(s); entry->next = NULL; free(vp); diff --git a/uip/mhfixmsg.c b/uip/mhfixmsg.c index 33faf468..e9ed4fc9 100644 --- a/uip/mhfixmsg.c +++ b/uip/mhfixmsg.c @@ -1078,8 +1078,9 @@ fix_composite_cte (CT ct, int *message_mods) { if (! strncasecmp (name, ENCODING_FIELD, strlen (ENCODING_FIELD))) { char *prefix = "Nmh-REPLACED-INVALID-"; - HF h = mh_xmalloc (sizeof *h); + HF h; + NEW(h); h->name = add (hf->name, NULL); h->hf_encoding = hf->hf_encoding; h->next = hf->next; @@ -1395,8 +1396,9 @@ find_textplain_sibling (CT parent, int replacetextplain, static int insert_new_text_plain_part (CT ct, int new_subpart_number, CT parent) { struct multipart *mp = (struct multipart *) parent->c_ctparams; - struct part *new_part = mh_xmalloc (sizeof *new_part); + struct part *new_part; + NEW(new_part); if ((new_part->mp_part = build_text_plain_part (ct))) { char buffer[16]; snprintf (buffer, sizeof buffer, "%d", new_subpart_number); @@ -1758,8 +1760,8 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) { add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm, "boundary", boundary, 0); - p = (struct part *) mh_xmalloc (sizeof *p); - p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next); + NEW(p); + NEW(p->mp_next); p->mp_next->mp_next = NULL; p->mp_next->mp_part = first_alt; diff --git a/uip/mhparse.c b/uip/mhparse.c index f43fd494..49c1aba6 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -581,7 +581,7 @@ add_header (CT ct, char *name, char *value) HF hp; /* allocate header field structure */ - hp = mh_xmalloc (sizeof(*hp)); + NEW(hp); /* link data into header structure */ hp->name = name; @@ -3537,7 +3537,7 @@ bad_quote: } if (pp == NULL) { - pp = mh_xmalloc(sizeof(*pp)); + NEW(pp); memset(pp, 0, sizeof(*pp)); pp->name = nameptr; pp->next = phead; @@ -3548,7 +3548,7 @@ bad_quote: * Insert this into the section linked list */ - sp = mh_xmalloc(sizeof(*sp)); + NEW(sp); memset(sp, 0, sizeof(*sp)); sp->value = valptr; sp->index = index; @@ -4082,10 +4082,10 @@ normal_param(PM pm, char *output, size_t len, size_t valuelen, PM add_param(PM *first, PM *last, char *name, char *value, int nocopy) { - PM pm = mh_xmalloc(sizeof(*pm)); + PM pm; + NEW(pm); memset(pm, 0, sizeof(*pm)); - pm->pm_name = nocopy ? name : getcpy(name); pm->pm_value = nocopy ? value : getcpy(value); diff --git a/uip/mhshowsbr.c b/uip/mhshowsbr.c index 093c9776..57ea7882 100644 --- a/uip/mhshowsbr.c +++ b/uip/mhshowsbr.c @@ -1359,14 +1359,14 @@ compile_marker(char *form) dispo_comp = comp; } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 && strlen(comp->c_name) > 6) { - pc_entry = mh_xmalloc(sizeof(*pc_entry)); + NEW(pc_entry); pc_entry->param = getcpy(comp->c_name + 6); pc_entry->comp = comp; pc_entry->next = ctype_pc_list; ctype_pc_list = pc_entry; } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 && strlen(comp->c_name) > 7) { - pc_entry = mh_xmalloc(sizeof(*pc_entry)); + NEW(pc_entry); pc_entry->param = getcpy(comp->c_name + 7); pc_entry->comp = comp; pc_entry->next = dispo_pc_list; diff --git a/uip/mhstoresbr.c b/uip/mhstoresbr.c index 460eac28..9869914b 100644 --- a/uip/mhstoresbr.c +++ b/uip/mhstoresbr.c @@ -42,8 +42,9 @@ struct mhstoreinfo { mhstoreinfo_t mhstoreinfo_create (CT *ct, char *pwd, const char *csw, int asw, int vsw) { - mhstoreinfo_t info = mh_xmalloc (sizeof *info); + mhstoreinfo_t info; + NEW(info); info->cts = ct; info->cwd = pwd; info->autosw = asw; diff --git a/uip/new.c b/uip/new.c index 9a7f9d4f..a8ee075e 100644 --- a/uip/new.c +++ b/uip/new.c @@ -201,9 +201,10 @@ check_folder(char *folder, size_t len, struct list_state *b) if (is_cur || msgnums != NULL) { if (*b->first == NULL) { - *b->first = b->node = mh_xmalloc(sizeof(*b->node)); + NEW(b->node); + *b->first = b->node; } else { - b->node->n_next = mh_xmalloc(sizeof(*b->node)); + NEW(b->node->n_next); b->node = b->node->n_next; } b->node->n_name = folder; diff --git a/uip/replsbr.c b/uip/replsbr.c index 6380884c..983fd28b 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -516,10 +516,11 @@ fix_addresses (char *str) { * local part. */ while ((cp = getname (str))) { - struct adr_node *adr_nodep = mh_xmalloc (sizeof *adr_nodep); + struct adr_node *adr_nodep; char error[BUFSIZ]; struct mailname *mp; + NEW(adr_nodep); adr_nodep->adr = strdup (cp); adr_nodep->escape_local_part = 0; adr_nodep->fixed = 0;