From 72795bfcbe9f7fee8927b1a4942c0230b0f857a8 Mon Sep 17 00:00:00 2001 From: David Levine Date: Sat, 11 Oct 2014 09:22:52 -0500 Subject: [PATCH] Added mh_xcalloc(). --- h/utils.h | 1 + sbr/addrsbr.c | 2 +- sbr/fmt_compile.c | 6 +++--- sbr/m_getfld.c | 2 +- sbr/utils.c | 20 +++++++++++++++++++- uip/ap.c | 2 +- uip/burst.c | 2 +- uip/mhlist.c | 4 ++-- uip/mhparse.c | 14 +++++++------- uip/mhshow.c | 4 ++-- uip/mhstore.c | 4 ++-- uip/scansbr.c | 6 +++--- uip/sortm.c | 6 +++--- 13 files changed, 46 insertions(+), 27 deletions(-) diff --git a/h/utils.h b/h/utils.h index a8059ea..6f74c32 100644 --- a/h/utils.h +++ b/h/utils.h @@ -4,6 +4,7 @@ void *mh_xmalloc(size_t); void *mh_xrealloc(void *, size_t); +void *mh_xcalloc(size_t, size_t); char *pwd(void); char *add(char *, char *); void create_folder(char *, int, void (*)(int)); diff --git a/sbr/addrsbr.c b/sbr/addrsbr.c index ea017f4..e5fc19d 100644 --- a/sbr/addrsbr.c +++ b/sbr/addrsbr.c @@ -103,7 +103,7 @@ getm(char *str, char *dfhost, int dftype, int wanthost, char *eresult) dftype = LOCALHOST; } - mp = (struct mailname *) calloc((size_t) 1, sizeof(*mp)); + mp = (struct mailname *) mh_xcalloc((size_t) 1, sizeof(*mp)); if (mp == NULL) { if (eresult) strcpy(eresult, "insufficient memory to represent address"); diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index 9e08ab4..28a96e6 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -208,7 +208,7 @@ static struct ftable functable[] = { /* Add new component to the hash table */ #define NEWCOMP(cm,name) do { \ - cm = ((struct comp *) calloc(1, sizeof (struct comp)));\ + cm = ((struct comp *) mh_xcalloc(1, sizeof (struct comp)));\ cm->c_name = name;\ ncomp++;\ i = CHASH(name);\ @@ -327,7 +327,7 @@ fmt_compile(char *fstring, struct format **fmt) i = strlen(fstring)/2 + 1; if (i == 1) i++; - next_fp = formatvec = (struct format *)calloc((size_t) i, + next_fp = formatvec = (struct format *)mh_xcalloc((size_t) i, sizeof(struct format)); if (next_fp == NULL) adios(EX_OSERR, NULL, "unable to allocate format storage"); @@ -480,7 +480,7 @@ do_name(char *sp, int preprocess) CERROR("component used as both date and address"); } cm->c_tws = (struct tws *) - calloc((size_t) 1, sizeof(*cm->c_tws)); + mh_xcalloc((size_t) 1, sizeof(*cm->c_tws)); fp->f_type = preprocess; PUTCOMP(sp); cm->c_type |= CT_DATE; diff --git a/sbr/m_getfld.c b/sbr/m_getfld.c index 1cc041b..3539ed4 100644 --- a/sbr/m_getfld.c +++ b/sbr/m_getfld.c @@ -628,7 +628,7 @@ thisisanmbox(FILE *iob) ** separator) or the last char (since the matchc would have found it ** if it was a real delim). */ - pat_map = (unsigned char **) calloc(256, sizeof(unsigned char *)); + pat_map = (unsigned char **) mh_xcalloc(256, sizeof(unsigned char *)); for (cp = (char *) fdelim + 1; cp < (char *) delimend; cp++ ) pat_map[(unsigned char)*cp] = (unsigned char *) cp; diff --git a/sbr/utils.c b/sbr/utils.c index 515c9a0..b0e8274 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -49,7 +49,7 @@ mh_xrealloc(void *ptr, size_t size) /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */ if (!ptr) { - return mh_xmalloc(size); + return mh_xcalloc((size_t) 1, size); } if (size == 0) @@ -63,6 +63,24 @@ mh_xrealloc(void *ptr, size_t size) } /* + * Safely call calloc + */ +void * +mh_xcalloc(size_t nmemb, size_t size) +{ + void *memory; + + if (nmemb == 0 || size == 0) + adios(EX_SOFTWARE, NULL, "Tried to calloc 0 bytes"); + + if ((memory = calloc(nmemb, size))) { + return memory; + } else { + adios(EX_OSERR, NULL, "calloc failed"); + } +} + +/* ** Return the present working directory, if the current directory does not ** exist, or is too long, make / the pwd. */ diff --git a/uip/ap.c b/uip/ap.c index acf3cac..46c3ff5 100644 --- a/uip/ap.c +++ b/uip/ap.c @@ -138,7 +138,7 @@ process(char *arg, int norm) (q = &pq)->pq_next = NULL; while ((cp = getname(arg))) { if ((p = (struct pqpair *) - calloc((size_t) 1, sizeof(*p))) == NULL) + mh_xcalloc((size_t) 1, sizeof(*p))) == NULL) adios(EX_OSERR, NULL, "unable to allocate pqpair memory"); if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) { p->pq_text = getcpy(cp); diff --git a/uip/burst.c b/uip/burst.c index 37dc9b1..28e0e85 100644 --- a/uip/burst.c +++ b/uip/burst.c @@ -117,7 +117,7 @@ main(int argc, char **argv) seq_setprev(mp); /* set the previous-sequence */ smsgs = (struct smsg *) - calloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs)); + mh_xcalloc((size_t) (MAXFOLDER + 2), sizeof(*smsgs)); if (smsgs == NULL) adios(EX_OSERR, NULL, "unable to allocate burst storage"); diff --git a/uip/mhlist.c b/uip/mhlist.c index c4682b4..9022ecf 100644 --- a/uip/mhlist.c +++ b/uip/mhlist.c @@ -197,7 +197,7 @@ main(int argc, char **argv) ** check if message is coming from file */ if (file) { - if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts)))) + if (!(cts = (CT *) mh_xcalloc((size_t) 2, sizeof(*cts)))) adios(EX_OSERR, NULL, "out of memory"); ctp = cts; @@ -232,7 +232,7 @@ main(int argc, char **argv) } seq_setprev(mp); /* set the previous-sequence */ - if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1), + if (!(cts = (CT *) mh_xcalloc((size_t) (mp->numsel + 1), sizeof(*cts)))) adios(EX_OSERR, NULL, "out of memory"); ctp = cts; diff --git a/uip/mhparse.c b/uip/mhparse.c index f520e03..2afb03a 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -238,7 +238,7 @@ get_content(FILE *in, char *file, int toplevel) HF hp; /* allocate the content structure */ - if (!(ct = (CT) calloc(1, sizeof(*ct)))) + if (!(ct = (CT) mh_xcalloc(1, sizeof(*ct)))) adios(EX_OSERR, NULL, "out of memory"); ct->c_fp = in; @@ -983,7 +983,7 @@ InitText(CT ct) ct->c_subtype = kv->kv_value; /* allocate text character set structure */ - if ((t = (struct text *) calloc(1, sizeof(*t))) == NULL) + if ((t = (struct text *) mh_xcalloc(1, sizeof(*t))) == NULL) adios(EX_OSERR, NULL, "out of memory"); ct->c_ctparams = (void *) t; @@ -1065,7 +1065,7 @@ InitMultiPart(CT ct) } /* allocate primary structure for multipart info */ - if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL) + if ((m = (struct multipart *) mh_xcalloc(1, sizeof(*m))) == NULL) adios(EX_OSERR, NULL, "out of memory"); ct->c_ctparams = (void *) m; @@ -1109,7 +1109,7 @@ InitMultiPart(CT ct) if (strcmp(buffer + 2, m->mp_start)!=0) continue; next_part: - if ((part = (struct part *) calloc(1, sizeof(*part))) + if ((part = (struct part *) mh_xcalloc(1, sizeof(*part))) == NULL) adios(EX_OSERR, NULL, "out of memory"); *next = part; @@ -1225,7 +1225,7 @@ reverse_parts(CT ct) i++; /* allocate array of pointers to the parts */ - if (!(base = (struct part **) calloc((size_t) (i + 1), sizeof(*base)))) + if (!(base = (struct part **) mh_xcalloc((size_t) (i + 1), sizeof(*base)))) adios(EX_OSERR, NULL, "out of memory"); bmp = base; @@ -1282,7 +1282,7 @@ InitMessage(CT ct) char **ap, **ep; struct partial *p; - if ((p = (struct partial *) calloc(1, sizeof(*p))) == NULL) + if ((p = (struct partial *) mh_xcalloc(1, sizeof(*p))) == NULL) adios(EX_OSERR, NULL, "out of memory"); ct->c_ctparams = (void *) p; @@ -1395,7 +1395,7 @@ init_encoding(CT ct, OpenCEFunc openfnx) { CE ce; - if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL) + if ((ce = (CE) mh_xcalloc(1, sizeof(*ce))) == NULL) adios(EX_OSERR, NULL, "out of memory"); ct->c_cefile = ce; diff --git a/uip/mhshow.c b/uip/mhshow.c index 9ae5bf0..f48fa99 100644 --- a/uip/mhshow.c +++ b/uip/mhshow.c @@ -249,7 +249,7 @@ main(int argc, char **argv) ** check if message is coming from file */ if (file) { - if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts)))) + if (!(cts = (CT *) mh_xcalloc((size_t) 2, sizeof(*cts)))) adios(EX_OSERR, NULL, "out of memory"); ctp = cts; @@ -304,7 +304,7 @@ main(int argc, char **argv) seq_setprev(mp); /* set the Previous-Sequence */ seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */ - if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1), + if (!(cts = (CT *) mh_xcalloc((size_t) (mp->numsel + 1), sizeof(*cts)))) adios(EX_OSERR, NULL, "out of memory"); ctp = cts; diff --git a/uip/mhstore.c b/uip/mhstore.c index 4234d25..2a0ddf9 100644 --- a/uip/mhstore.c +++ b/uip/mhstore.c @@ -261,7 +261,7 @@ main(int argc, char **argv) ** check if message is coming from file */ if (file) { - if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts)))) + if (!(cts = (CT *) mh_xcalloc((size_t) 2, sizeof(*cts)))) adios(EX_OSERR, NULL, "out of memory"); ctp = cts; @@ -294,7 +294,7 @@ main(int argc, char **argv) exit(EX_USAGE); seq_setprev(mp); /* set the previous-sequence */ - if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1), + if (!(cts = (CT *) mh_xcalloc((size_t) (mp->numsel + 1), sizeof(*cts)))) adios(EX_OSERR, NULL, "out of memory"); ctp = cts; diff --git a/uip/scansbr.c b/uip/scansbr.c index 9c74f04..ade3a28 100644 --- a/uip/scansbr.c +++ b/uip/scansbr.c @@ -98,11 +98,11 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg, datecomp = NULL; } - nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, + nxtbuf = compbuffers = (char **) mh_xcalloc((size_t) ncomps, sizeof(char *)); if (!nxtbuf) adios(EX_OSERR, NULL, "unable to allocate component buffers"); - used_buf = (struct comp **) calloc((size_t) (ncomps+1), + used_buf = (struct comp **) mh_xcalloc((size_t) (ncomps+1), sizeof(struct comp *)); if (!used_buf) adios(EX_OSERR, NULL, "unable to allocate component buffer stack"); @@ -263,7 +263,7 @@ finished: if (datecomp && !datecomp->c_text) { if (!datecomp->c_text) { if (!datecomp->c_tws) - datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws)); + datecomp->c_tws = (struct tws *) mh_xcalloc((size_t) 1, sizeof(*datecomp->c_tws)); if (!datecomp->c_tws) adios(EX_OSERR, NULL, "unable to allocate tws buffer"); *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime); diff --git a/uip/sortm.c b/uip/sortm.c index 0d02af9..ae4038d 100644 --- a/uip/sortm.c +++ b/uip/sortm.c @@ -197,7 +197,7 @@ main(int argc, char **argv) /* ** sort a list of pointers to our "messages to be sorted". */ - dlist = (struct smsg **) mh_xmalloc((nmsgs+1) * sizeof(*dlist)); + dlist = (struct smsg **) mh_xcalloc((size_t) (nmsgs+1), sizeof(*dlist)); for (i = 0; i < nmsgs; i++) dlist[i] = &smsgs[i]; dlist[nmsgs] = 0; @@ -235,7 +235,7 @@ main(int argc, char **argv) ** the collection of messages with the same subj ** given a message number. */ - il = (struct smsg ***) calloc(mp->hghsel+1, sizeof(*il)); + il = (struct smsg ***) mh_xcalloc(mp->hghsel+1, sizeof(*il)); if (! il) adios(EX_OSERR, NULL, "couldn't allocate msg list"); for (i = 0; i < nmsgs; i++) @@ -295,7 +295,7 @@ read_hdrs(struct msgs *mp, char *datesw) twscopy(&tb, dlocaltimenow()); - smsgs = (struct smsg *) calloc((size_t) (mp->hghsel - mp->lowsel + 2), + smsgs = (struct smsg *) mh_xcalloc((size_t) (mp->hghsel - mp->lowsel + 2), sizeof(*smsgs)); if (smsgs == NULL) adios(EX_OSERR, NULL, "unable to allocate sort storage"); -- 1.7.10.4