calling realloc.
2006-01-01 Josh Bressers <josh@bress.net>
+ * patch #3967: Create a mh_xrealloc function to prevent mistakes when
+ calling realloc.
+
+2006-01-01 Josh Bressers <josh@bress.net>
+
* patch #3966: Create a mh_xmalloc function to prevent mistakes when
calling malloc.
*/
void *mh_xmalloc(size_t);
-
+void *mh_xrealloc(void *, size_t);
/* enlarge pointer array, if necessary */
if (i >= len) {
len += NUMBROKEN;
- if (!(broken = realloc (broken, (size_t) (len * sizeof(*broken)))))
- adios (NULL, "unable to realloc array in brkstring");
+ broken = mh_xrealloc (broken, (size_t) (len * sizeof(*broken)));
}
while (brkany (c = *s, brksep))
int i = dst - buf;\
int n = last_dst - buf;\
bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
- buf = realloc (buf, bufsiz);\
+ buf = mh_xrealloc (buf, bufsiz);\
dst = buf + i;\
last_dst = buf + n;\
- if (! buf)\
- adios (NULL, "formataddr: couldn't get buffer space");\
bufend = buf + bufsiz;\
}
*/
if (mp->nummsg >= len) {
len += NUMMSGS;
- if (!(mi = (int *) realloc (mi,
- (size_t) (len * sizeof(*mi))))) {
- adios (NULL, "unable to allocate storage");
- }
+ mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
}
/* Check if this is the first message we've seen */
* status array. So we don't have to move anything and can
* just realloc the message status array.
*/
- if (!(mp->msgstats = realloc (mp->msgstats, MSGSTATSIZE(mp, lo, hi)))) {
- advise (NULL, "unable to reallocate message storage");
- return NULL;
- }
+ mp->msgstats = mh_xrealloc (mp->msgstats, MSGSTATSIZE(mp, lo, hi));
} else {
/*
* We are changing the offset of the message status
break;
}
if (cp >= ep) {
- if (!(dp = realloc (pp, (size_t) (len += BUFSIZ)))) {
- free (pp);
- pp = NULL;
- return NOTOK;
- }
- else
- cp += dp - pp, ep = (pp = cp) + len - 2;
+ dp = mh_xrealloc (pp, (size_t) (len += BUFSIZ));
+ cp += dp - pp, ep = (pp = cp) + len - 2;
}
}
}
char *newbuf;
len += MAXBUFFER;
- if (!(newbuf = realloc (buffer, (size_t) len)))
- adios (NULL, "unable to realloc storage in seq_list");
+ newbuf = mh_xrealloc (buffer, (size_t) len);
bp = newbuf + (bp - buffer);
buffer = newbuf;
}
return memory;
}
+
+void *
+mh_xrealloc(void *ptr, size_t size)
+{
+ void *memory;
+
+ if (size == 0)
+ adios(NULL, "Tried to realloc 0bytes");
+
+ memory = realloc(ptr, size);
+ if (!memory)
+ adios(NULL, "Realloc failed");
+
+ return memory;
+}
if (cp >= ep) {
int curlen = cp - pp;
- if (!(dp = realloc (pp, (size_t) (len += BUFSIZ)))) {
- adios (NULL, "unable to allocate string storage");
- } else {
- cp = dp + curlen;
- ep = (pp = dp) + len - 1;
- }
+ dp = mh_xrealloc (pp, (size_t) (len += BUFSIZ));
+ cp = dp + curlen;
+ ep = (pp = dp) + len - 1;
}
}
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
}
if (++n >= field_size - 1) {
- if ((field = (char *)realloc((void *)field, field_size += 256)) == (char *)0)
- adios(NULL, "can't grow field buffer.");
+ field = (char *) mh_xrealloc((void *)field, field_size += 256);
cp = field + n - 1;
}
}
if (++n >= field_size - 1) {
- if ((field = (char *)realloc((void *)field, field_size *= 2)) == (char *)0)
- adios(NULL, "can't grow field buffer.");
+ field = (char *) mh_xrealloc((void *)field, field_size *= 2);
cp = field + n - 1;
}
if (i >= numgroups) {
if (numgroups >= maxgroups) {
maxgroups += NGRPS;
- if (!(grps = (char **) realloc(grps,
- (size_t) (maxgroups * sizeof(*grps)))))
- adios (NULL, "unable to reallocate group name storage");
+ grps = (char **) mh_xrealloc(grps,
+ (size_t) (maxgroups * sizeof(*grps)));
}
grps[numgroups++] = getcpy (gr->gr_name);
}
*/
#include <h/nmh.h>
+#include <h/utils.h>
#ifndef MMDFONLY
# include <h/mh.h>
if (dp >= ep) {
register int curlen = dp - pp;
- cp = (struct drop *) realloc ((char *) pp,
+ cp = (struct drop *) mh_xrealloc ((char *) pp,
(size_t) (len += MAXFOLDER) * sizeof(*pp));
- if (cp == NULL) {
- if (noisy)
- admonish (NULL, "unable to allocate drop storage");
- free ((char *) pp);
- return 0;
- }
dp = cp + curlen, ep = (pp = cp) + len - 1;
}
}
*/
if (numfolders >= maxfolders) {
maxfolders += MAXFOLDERS;
- if (!(foldersToDo = (char **) realloc (foldersToDo,
- (size_t) (maxfolders * sizeof(*foldersToDo)))))
- adios (NULL, "unable to reallocate folder name storage");
+ foldersToDo = (char **) mh_xrealloc (foldersToDo,
+ (size_t) (maxfolders * sizeof(*foldersToDo)));
}
if (*cp == '+' || *cp == '@') {
foldersToDo[numfolders++] =
*f = (struct Folder *) mh_xmalloc (*nfa * (sizeof(struct Folder)));
} else {
*nfa *= 2;
- *f = (struct Folder *) realloc (*f, *nfa * (sizeof(struct Folder)));
+ *f = (struct Folder *) mh_xrealloc (*f, *nfa * (sizeof(struct Folder)));
}
}
*/
if (total_folders >= maxFolderInfo) {
maxFolderInfo += NUMFOLDERS;
- if ((fi = realloc (fi, maxFolderInfo * sizeof(*fi))) == NULL)
- adios (NULL, "unable to re-allocate storage for folder info");
+ fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
}
fi[i].name = fold;
/* if necessary, reallocate the space for folder names */
if (foldp >= maxfolders) {
maxfolders += NUMFOLDERS;
- if ((folds = realloc (folds, maxfolders * sizeof(char *))) == NULL)
- adios (NULL, "unable to re-allocate storage for folder names");
+ folds = mh_xrealloc (folds, maxfolders * sizeof(char *));
}
cp = getcpy (fold);
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
if (cp) {
int j;
char *dp;
- if ((dp = realloc (cp, (unsigned) (j = len + i))) == NULL)
- adios (NULL, "unable to allocate face storage");
+ dp = mh_xrealloc (cp, (unsigned) (j = len + i));
memcpy(dp + len, buffer, i);
cp = dp, len = j;
}
#include <h/mime.h>
#include <h/mhparse.h>
#include <h/mhcachesbr.h>
+#include <h/utils.h>
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage ");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
#include <h/scansbr.h>
#include <h/tws.h>
#include <h/mts.h>
+#include <h/utils.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
return;
nmsgs = nMsgs + n + MAXFOLDER / 2;
- Msgs = (struct Msg *) realloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
- if (Msgs == NULL)
- padios (NULL, "unable to reallocate Msgs structure");
+ Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
nMsgs = nmsgs;
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
}
if (retbufsize > size) {
- buffer = realloc(buffer, retbufsize);
- if (!buffer) {
- snprintf(response, sizeof(response), "Error during realloc in "
- "read routine: %s", strerror(errno));
- return -2;
- }
+ buffer = mh_xrealloc(buffer, retbufsize);
size = retbufsize;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
int i = dst - buf;\
int n = last_dst - buf;\
bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
- buf = realloc (buf, bufsiz);\
+ buf = mh_xrealloc (buf, bufsiz);\
dst = buf + i;\
last_dst = buf + n;\
- if (! buf)\
- adios (NULL, "formataddr: couldn't get buffer space");\
bufend = buf + bufsiz;\
}
*/
if (nummsgs >= maxmsgs){
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}
}
if (++n >= field_size - 1) {
- if ((field = (char *)realloc((void *)field, field_size += 256)) == (char *)0)
- adios(NULL, "can't grow field buffer.");
+ field = (char *)mh_xrealloc((void *)field, field_size += 256);
p = field + n - 1;
}
*/
if (nummsgs >= maxmsgs) {
maxmsgs += MAXMSGS;
- if (!(msgs = (char **) realloc (msgs,
- (size_t) (maxmsgs * sizeof(*msgs)))))
- adios (NULL, "unable to reallocate msgs storage");
+ msgs = (char **) mh_xrealloc (msgs,
+ (size_t) (maxmsgs * sizeof(*msgs)));
}
msgs[nummsgs++] = cp;
}