calloc() is safer, because it initialize the memory to zero.
** utils.h -- utility prototypes
*/
-void *mh_xmalloc(size_t);
void *mh_xrealloc(void *, size_t);
void *mh_xcalloc(size_t, size_t);
char *pwd(void);
/* allocate initial space for pointers on first call */
if (!broken) {
len = NUMBROKEN;
- broken = (char **)mh_xmalloc((size_t)(len * sizeof(*broken)));
+ broken = (char **)mh_xcalloc((size_t)len, sizeof(*broken));
}
/*
len += strlen(cp);
va_end(list);
- dp = sp = mh_xmalloc(len);
+ dp = sp = mh_xcalloc(len, sizeof(char));
sp = copy(s1, sp);
** If list is emtpy, allocate head of profile/context list.
*/
if (!m_defs) {
- m_defs = (struct node *) mh_xmalloc(sizeof(*np));
+ m_defs = (struct node *) mh_xcalloc(1, sizeof(*np));
np = m_defs;
np->n_name = getcpy(key);
/*
** Else add this new entry at the end
*/
- np->n_next = (struct node *) mh_xmalloc(sizeof(*np));
+ np->n_next = (struct node *) mh_xcalloc(1, sizeof(*np));
np = np->n_next;
np->n_name = getcpy(key);
void
crawl_folders(char *dir, crawl_callback_t *callback, void *baton)
{
- struct crawl_context *crawl = mh_xmalloc(sizeof(*crawl));
+ struct crawl_context *crawl = mh_xcalloc(1, sizeof(*crawl));
crawl->max = CRAWL_NUMFOLDERS;
crawl->total = crawl->start = crawl->foldp = 0;
- crawl->folders = mh_xmalloc(crawl->max * sizeof(*crawl->folders));
+ crawl->folders = mh_xcalloc(crawl->max, sizeof(*crawl->folders));
crawl_folders_body(crawl, dir, callback, baton);
** least one) until we get to the actual
** text. Copy until we get to a non-space.
*/
- output = mh_xmalloc(outlen);
+ output = mh_xcalloc(outlen, sizeof(char));
q = output;
while (is_fws(*p)) {
*q++ = *p++;
static void
unfold_header(char **value, int len)
{
- char *str = mh_xmalloc(len + 1);
+ char *str = mh_xcalloc(len + 1, sizeof(char));
char *p = str, *q = *value;
while (*q != '\0') {
/* if we don't have a buffer yet, get one */
if (bufsiz == 0) {
- buf = mh_xmalloc(BUFINCR);
+ buf = mh_xcalloc(BUFINCR, sizeof(char));
last_dst = buf; /* XXX */
bufsiz = BUFINCR - 6; /* leave some slop */
bufend = buf + bufsiz;
if (fstat(fileno(fp), &st) == -1) {
adios(EX_IOERR, form, "unable to stat format file");
}
- formats = mh_xmalloc((size_t) st.st_size + 1);
+ formats = mh_xcalloc((size_t) st.st_size + 1, sizeof(char));
if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
adios(EX_IOERR, form, "error reading format file");
}
if (fstat(fileno(fp), &st) == -1) {
adios(EX_IOERR, def_form, "unable to stat format file");
}
- formats = mh_xmalloc((size_t) st.st_size + 1);
+ formats = mh_xcalloc((size_t) st.st_size + 1, sizeof(char));
if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
adios(EX_IOERR, def_form, "error reading format file");
}
if (use_iconv) {
saveq = q;
savedstlen = dstlen;
- q = convbuf = (char *) mh_xmalloc(endofmime - startofmime);
+ q = convbuf = (char *) mh_xcalloc(endofmime - startofmime, sizeof(char));
}
/*
** ADDCHR2 is for adding characters when q is or might be convbuf:
}
/* Allocate the main structure for folder information */
- mp = (struct msgs *) mh_xmalloc((size_t) sizeof(*mp));
+ mp = (struct msgs *) mh_xcalloc(1, (size_t) sizeof(*mp));
clear_folder_flags(mp);
mp->foldpath = name;
** name of the messages in this folder.
*/
len = NUMMSGS;
- mi = (int *) mh_xmalloc((size_t) (len * sizeof(*mi)));
+ mi = (int *) mh_xcalloc((size_t) len, sizeof(*mi));
while ((dp = readdir(dd))) {
if ((msgnum = m_atoi(dp->d_name)) && msgnum > 0) {
/* Allocate space for status of each message. */
- mp->msgstats = mh_xmalloc(MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
+ mp->msgstats = mh_xcalloc(MSGSTATSIZE(mp, mp->lowoff, mp->hghoff), 1);
/*
** Clear all the flag bits for all the message
seqset_t *tmpstats;
/* first allocate the new message status space */
- tmpstats = mh_xmalloc(MSGSTATSIZE(mp, lo, hi));
+ tmpstats = mh_xcalloc(MSGSTATSIZE(mp, lo, hi), 1);
/* then copy messages status array with shift */
if (mp->nummsg > 0) {
n++;
}
- arguments = (char **) mh_xmalloc((argc + n) * sizeof(*arguments));
+ arguments = (char **) mh_xcalloc(argc + n, sizeof(*arguments));
bp = arguments;
/* Copy any arguments from profile/context */
if (str) {
len = strlen(str) + 1;
- cp = mh_xmalloc(len);
+ cp = mh_xcalloc(len, sizeof(char));
memcpy(cp, str, len);
} else {
- cp = mh_xmalloc((size_t) 1);
+ cp = mh_xcalloc((size_t) 1, sizeof(char));
*cp = '\0';
}
return cp;
struct lock *lp;
size_t len;
- lp = (struct lock *) mh_xmalloc(sizeof(*lp));
+ lp = (struct lock *) mh_xcalloc(1, sizeof(*lp));
len = strlen(curlock) + 1;
lp->l_fd = fd;
- lp->l_lock = mh_xmalloc(len);
+ lp->l_lock = mh_xcalloc(len, sizeof(char));
memcpy(lp->l_lock, curlock, len);
lp->l_next = l_top;
continue;
}
c = strlen(delimstr);
- fdelim = (unsigned char *) mh_xmalloc((size_t) (c + 3));
+ fdelim = (unsigned char *) mh_xcalloc((size_t) (c + 3), sizeof(char));
*fdelim++ = '\0';
*fdelim = '\n';
msg_delim = (char *)fdelim+1;
for(;;)
pause();
}
- p = mh_xmalloc((size_t) (strlen(s) + 2));
+ p = mh_xcalloc((size_t) (strlen(s) + 2), sizeof(char));
strcpy(p, s);
return p;
}
int i;
char **ep, **nep, *cp;
- cp = mh_xmalloc((size_t) (strlen(name) + strlen(value) + 2));
+ cp = mh_xcalloc((size_t) (strlen(name) + strlen(value) + 2), sizeof(char));
sprintf(cp, "%s=%s", name, value);
return 0;
}
- nep = (char **) mh_xmalloc((size_t) ((i + 2) * sizeof(*nep)));
+ nep = (char **) mh_xcalloc((size_t) (i + 2), sizeof(*nep));
for (ep = environ, i = 0; *ep; nep[i++] = *ep++)
continue;
ib)) {
case FLD:
case FLDPLUS:
- np = (struct node *) mh_xmalloc(sizeof(*np));
+ np = (struct node *) mh_xcalloc(1, sizeof(*np));
*npp = np;
*(npp = &np->n_next) = NULL;
np->n_name = getcpy(name);
/* On first invocation, allocate initial buffer space */
if (!buffer) {
len = MAXBUFFER;
- buffer = mh_xmalloc((size_t) len);
+ buffer = mh_xcalloc((size_t) len, sizeof(char));
}
/*
#define MAXMSGS 256
/*
-** Safely call malloc
-*/
-void *
-mh_xmalloc(size_t size)
-{
- void *memory;
-
- if (size == 0) {
- adios(EX_SOFTWARE, NULL, "Tried to malloc 0 bytes");
- }
-
- memory = malloc(size);
- if (!memory) {
- adios(EX_OSERR, NULL, "Malloc failed");
- }
-
- return memory;
-}
-
-/*
** Safely call realloc
*/
void *
len2 = strlen(s2);
}
- cp = mh_xmalloc(len1 + len2 + 1);
+ cp = mh_xcalloc(len1 + len2 + 1, sizeof(char));
/* Copy s1 and free it */
if (s1) {
static char *pp = NULL;
if (pp == NULL)
- pp = mh_xmalloc((size_t) (len = BUFSIZ));
+ pp = mh_xcalloc((size_t) (len = BUFSIZ), sizeof(char));
for (ep = (cp = pp) + len - 1;;) {
if (fgets(cp, ep - cp + 1, in) == NULL) {
if (strcmp(pp, ad->ad_text)==0)
return;
- ad = (struct adr *) mh_xmalloc(sizeof(*ad));
+ ad = (struct adr *) mh_xcalloc(1, sizeof(*ad));
ad->ad_text = getcpy(pp);
ad->ad_local = strchr(pp, '@') == NULL;
ad->ad_next = NULL;
{
struct aka *p;
- p = (struct aka *) mh_xmalloc(sizeof(*p));
+ p = (struct aka *) mh_xcalloc(1, sizeof(*p));
p->ak_name = getcpy(id);
p->ak_visible = 0;
{
struct home *p;
- p = (struct home *) mh_xmalloc(sizeof(*p));
+ p = (struct home *) mh_xcalloc(1, sizeof(*p));
p->h_name = getcpy(pw->pw_name);
p->h_uid = pw->pw_uid;
}
/* We'll grow this buffer as needed. */
- field = (char *)mh_xmalloc(field_size = 256);
+ field = (char *)mh_xcalloc(field_size = 256, sizeof(char));
make_comp(&comp);
length = strlen(comp); /* Convenience copy. */
if ((fp = fdopen(fd, "r")) == NULL) {
adios(EX_IOERR, NULL, "unable to fdopen file.");
}
- field = (char *)mh_xmalloc(field_size);
+ field = (char *)mh_xcalloc(field_size, sizeof(char));
/*
** Copy lines from the input file to the temporary file
/* allocate the initial space to record the folder names */
numfolders = 0;
maxfolders = MAXFOLDERS;
- foldersToDo = (char **) mh_xmalloc((size_t)
- (maxfolders * sizeof(*foldersToDo)));
+ foldersToDo = (char **) mh_xcalloc((size_t) maxfolders, sizeof(*foldersToDo));
/* no sequences yet */
numsequences = 0;
AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
o = &orders[nOrders++];
o->priority = priority++;
- o->name = (char *) mh_xmalloc(p - s + 1);
+ o->name = (char *) mh_xcalloc(p - s + 1, sizeof(char));
strncpy(o->name, s, p - s);
o->name[p - s] = 0;
} else
return;
if (*f == NULL) {
*nfa = 10;
- *f = (struct Folder *) mh_xmalloc(
- *nfa * (sizeof(struct Folder)));
+ *f = (struct Folder *) mh_xcalloc(*nfa, sizeof(struct Folder));
} else {
*nfa *= 2;
*f = (struct Folder *) mh_xrealloc(
/* Allocate initial space to record folder information */
maxFolderInfo = CRAWL_NUMFOLDERS;
- fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
+ fi = mh_xcalloc(maxFolderInfo, sizeof(*fi));
/*
** Scan the folders
if ((in = dup(fileno(tmp))) == NOTOK)
adios(EX_OSERR, "dup", "unable to");
- line = mh_xmalloc((unsigned) fmtsize);
+ line = mh_xcalloc((unsigned) fmtsize, sizeof(char));
fmt_scan(fmt, line, fmtsize, dat);
fputs(line, tmp);
free(line);
continue;
}
if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
- holder.c_text = mh_xmalloc(sizeof(buf));
+ holder.c_text = mh_xcalloc(sizeof(buf), sizeof(char));
strncpy(holder.c_text, buf, sizeof(buf));
while (state == BODY) {
putcomp(c1, &holder, BODYCOMP);
HF hp;
/* allocate header field structure */
- hp = mh_xmalloc(sizeof(*hp));
+ hp = mh_xcalloc(1, sizeof(*hp));
/* link data into header structure */
hp->name = name;
for (; *cp != '"'; ++cp)
;
- extracted_name_value = mh_xmalloc(cp - name_suffix_begin + 1);
+ extracted_name_value = mh_xcalloc(cp - name_suffix_begin + 1, sizeof(char));
memcpy(extracted_name_value, name_suffix_begin,
cp - name_suffix_begin);
extracted_name_value[cp - name_suffix_begin] = '\0';
if (is_cur || msgnums != NULL) {
if (*b->first == NULL) {
- *b->first = b->node = mh_xmalloc(sizeof(*b->node));
+ *b->first = b->node = mh_xcalloc(1, sizeof(*b->node));
} else {
- b->node->n_next = mh_xmalloc(sizeof(*b->node));
+ b->node->n_next = mh_xcalloc(1, sizeof(*b->node));
b->node = b->node->n_next;
}
b->node->n_name = folder;
for (i = 0; sequences[i] != NULL; i++) {
len += strlen(sequences[i]) + 1;
}
- result = mh_xmalloc(len + 1);
+ result = mh_xcalloc(len + 1, sizeof(char));
for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
len = strlen(sequences[i]);
*--savecomp = 0;
for (i = ncomps; i--;) {
- *nxtbuf++ = mh_xmalloc(SBUFSIZ);
+ *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
}
nxtbuf = compbuffers;
tmpbuf = *nxtbuf++;
finished: ;
i = format_len + char_read + 256;
- scanl = mh_xmalloc((size_t) i + 2);
+ scanl = mh_xcalloc((size_t) i + 2, sizeof(char));
dat[0] = dat[1] = dat[2] = dat[4] = 0;
dat[3] = OUTPUTLINELEN;
fmt_scan(fmt, scanl, i, dat);
*--savecomp = NULL; /* point at zero'd end minus 1 */
for (i = ncomps; i--; )
- *nxtbuf++ = mh_xmalloc(SBUFSIZ);
+ *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
nxtbuf = compbuffers; /* point at start */
tmpbuf = *nxtbuf++;
}
}
i = format_len + char_read + 256;
- scanl = mh_xmalloc((size_t) i + 2);
+ scanl = mh_xcalloc((size_t) i + 2, sizeof(char));
dat[0] = 0;
dat[1] = 0;
dat[2] = 0;
/* if we don't have a buffer yet, get one */
if (bufsiz == 0) {
- buf = mh_xmalloc(BUFINCR);
+ buf = mh_xcalloc(BUFINCR, sizeof(char));
last_dst = buf; /* XXX */
bufsiz = BUFINCR - 6; /* leave some slop */
bufend = buf + bufsiz;
adios(EX_SOFTWARE, NULL, "more than %d messages for refile exec",
MAXARGS - 6);
}
- vec = (char **)mh_xmalloc((size_t)(msgs.size + 6) * sizeof(*vec));
+ vec = (char **)mh_xcalloc((size_t)(msgs.size + 6), sizeof(*vec));
vec[vecp++] = "refile";
vec[vecp++] = "-src";
vec[vecp++] = concat("+", folder, NULL);
width = MAXSCANL;
}
dat[3] = slwidth = width;
- scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
- (slwidth + 2)); /* probably for \n and \0 */
+ scanl = (char *) mh_xcalloc((size_t)(slwidth + 2),
+ SCAN_CHARWIDTH); /* probably for \n and \0 */
/* Compile format string */
ncomps = fmt_compile(fmtstr, &fmt) + 1;
FINDCOMP(datecomp, "date");
*used_buf = NULL;
/* allocate space for the items */
for (i = ncomps; i--; )
- *nxtbuf++ = mh_xmalloc(SBUFSIZ);
+ *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
}
/*
}
/* We'll grow the buffer as needed. */
- field = (char *)mh_xmalloc(field_size = 256);
+ field = (char *)mh_xcalloc(field_size = 256, sizeof(char));
/*
** Scan the draft file for an attachment header field name.
}
/* We'll grow the buffer as needed. */
- field = (char *)mh_xmalloc(field_size = 256);
+ field = (char *)mh_xcalloc(field_size = 256, sizeof(char));
/* Scan the draft file for an attachment header field name. */
while (get_line() != EOF && *field != '\0' && *field != '-') {
struct smsg **slist, **flist;
struct smsg ***il, **fp, **dp;
- slist = (struct smsg **)
- mh_xmalloc((nmsgs+1) * sizeof(*slist));
+ slist = (struct smsg **) mh_xcalloc(nmsgs+1, sizeof(*slist));
memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
qsort((char *)slist, nmsgs, sizeof(*slist),
(qsort_comp) subsort);
** make up the final list, chronological but with
** all the same subjects grouped together.
*/
- flist = (struct smsg **)
- mh_xmalloc((nmsgs+1) * sizeof(*flist));
+ flist = (struct smsg **) mh_xcalloc(nmsgs+1, sizeof(*flist));
fp = flist;
for (dp = dlist; *dp;) {
struct smsg **s = il[(*dp++)->s_msg];
adios(EX_DATAERR, NULL, "message has no recipients");
}
- sargv = mh_xmalloc(sizeof(char **) * (recipientsc + 4));
+ sargv = mh_xcalloc(recipientsc + 4, sizeof(char **));
argp = sargv;
*argp++ = "send-mail";