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));
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");
/* 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);\
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");
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;
** 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;
/* 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)
}
/*
+ * 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.
*/
(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);
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");
** 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;
}
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;
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;
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;
}
/* 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;
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;
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;
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;
{
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;
** 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;
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;
** 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;
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;
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");
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);
/*
** 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;
** 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++)
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");