From: Josh Bressers Date: Mon, 2 Jan 2006 03:25:18 +0000 (+0000) Subject: * patch #3967: Create a mh_xrealloc function to prevent mistakes when X-Git-Tag: nmh-1_3_RC1~79 X-Git-Url: http://git.marmaro.de/?a=commitdiff_plain;h=008837e090c008e3afe7a9c8667070bafa091e62;p=mmh * patch #3967: Create a mh_xrealloc function to prevent mistakes when calling realloc. --- diff --git a/ChangeLog b/ChangeLog index f8d1c8d..757d2eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2006-01-01 Josh Bressers + * patch #3967: Create a mh_xrealloc function to prevent mistakes when + calling realloc. + +2006-01-01 Josh Bressers + * patch #3966: Create a mh_xmalloc function to prevent mistakes when calling malloc. diff --git a/h/utils.h b/h/utils.h index aba8fc4..8648b3f 100644 --- a/h/utils.h +++ b/h/utils.h @@ -6,4 +6,4 @@ */ void *mh_xmalloc(size_t); - +void *mh_xrealloc(void *, size_t); diff --git a/sbr/brkstring.c b/sbr/brkstring.c index 255abf7..d2f71aa 100644 --- a/sbr/brkstring.c +++ b/sbr/brkstring.c @@ -48,8 +48,7 @@ brkstring (char *str, char *brksep, char *brkterm) /* 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)) diff --git a/sbr/fmt_addr.c b/sbr/fmt_addr.c index 97f4271..d71d956 100644 --- a/sbr/fmt_addr.c +++ b/sbr/fmt_addr.c @@ -29,11 +29,9 @@ static unsigned int bufsiz; /* current size of buf */ 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;\ } diff --git a/sbr/folder_read.c b/sbr/folder_read.c index 8e3a3ff..0dec537 100644 --- a/sbr/folder_read.c +++ b/sbr/folder_read.c @@ -76,10 +76,7 @@ folder_read (char *name) */ 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 */ diff --git a/sbr/folder_realloc.c b/sbr/folder_realloc.c index 753da2e..e7c4bc4 100644 --- a/sbr/folder_realloc.c +++ b/sbr/folder_realloc.c @@ -47,10 +47,7 @@ folder_realloc (struct msgs *mp, int lo, int hi) * 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 diff --git a/sbr/mf.c b/sbr/mf.c index 9a2c647..518a184 100644 --- a/sbr/mf.c +++ b/sbr/mf.c @@ -975,13 +975,8 @@ mfgets (FILE *in, char **bp) 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; } } } diff --git a/sbr/seq_list.c b/sbr/seq_list.c index ecc03bf..f5dbb65 100644 --- a/sbr/seq_list.c +++ b/sbr/seq_list.c @@ -75,8 +75,7 @@ seq_list(struct msgs *mp, char *seqname) 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; } diff --git a/sbr/utils.c b/sbr/utils.c index bc17177..7dfe900 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -27,3 +27,18 @@ mh_xmalloc(size_t size) 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; +} diff --git a/sbr/vfgets.c b/sbr/vfgets.c index beb0afd..c6a39ea 100644 --- a/sbr/vfgets.c +++ b/sbr/vfgets.c @@ -66,12 +66,9 @@ wrong_guess: 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; } } } diff --git a/uip/anno.c b/uip/anno.c index d90e0f6..9fbb37f 100644 --- a/uip/anno.c +++ b/uip/anno.c @@ -233,9 +233,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/annosbr.c b/uip/annosbr.c index 337a01b..2ad873e 100644 --- a/uip/annosbr.c +++ b/uip/annosbr.c @@ -120,8 +120,7 @@ annolist(char *file, char *comp, char *text, int number) } 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; } @@ -262,8 +261,7 @@ annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw, in } 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; } diff --git a/uip/conflict.c b/uip/conflict.c index 5097119..1713b15 100644 --- a/uip/conflict.c +++ b/uip/conflict.c @@ -205,9 +205,8 @@ grp_names (void) 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); } diff --git a/uip/dropsbr.c b/uip/dropsbr.c index 7f432ea..dae1c3b 100644 --- a/uip/dropsbr.c +++ b/uip/dropsbr.c @@ -10,6 +10,7 @@ */ #include +#include #ifndef MMDFONLY # include @@ -224,14 +225,8 @@ mbx_read (FILE *fp, long pos, struct drop **drops, int noisy) 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; } } diff --git a/uip/flist.c b/uip/flist.c index 1e850cb..758d308 100644 --- a/uip/flist.c +++ b/uip/flist.c @@ -228,9 +228,8 @@ main(int argc, char **argv) */ 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++] = @@ -655,7 +654,7 @@ AllocFolders(struct Folder **f, int *nfa, int n) *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))); } } diff --git a/uip/folder.c b/uip/folder.c index 3348ae5..d6cb5b5 100644 --- a/uip/folder.c +++ b/uip/folder.c @@ -469,8 +469,7 @@ get_folder_info (char *fold, char *msg) */ 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; @@ -774,8 +773,7 @@ addfold (char *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); diff --git a/uip/mark.c b/uip/mark.c index 27652e0..0333722 100644 --- a/uip/mark.c +++ b/uip/mark.c @@ -159,9 +159,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/mhlist.c b/uip/mhlist.c index 56d9398..03480b3 100644 --- a/uip/mhlist.c +++ b/uip/mhlist.c @@ -264,9 +264,8 @@ do_cache: */ 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; } diff --git a/uip/mhlsbr.c b/uip/mhlsbr.c index af3fa55..31c24bb 100644 --- a/uip/mhlsbr.c +++ b/uip/mhlsbr.c @@ -1644,8 +1644,7 @@ doface (struct mcomp *c1) 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; } diff --git a/uip/mhn.c b/uip/mhn.c index 27ee05b..685a74e 100644 --- a/uip/mhn.c +++ b/uip/mhn.c @@ -20,6 +20,7 @@ #include #include #include +#include #ifdef HAVE_SYS_WAIT_H # include @@ -448,9 +449,8 @@ do_cache: */ 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; } diff --git a/uip/mhpath.c b/uip/mhpath.c index 5084b03..4a88605 100644 --- a/uip/mhpath.c +++ b/uip/mhpath.c @@ -89,9 +89,8 @@ main(int argc, char **argv) */ 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; } diff --git a/uip/mhshow.c b/uip/mhshow.c index c61e917..909c440 100644 --- a/uip/mhshow.c +++ b/uip/mhshow.c @@ -305,9 +305,8 @@ do_cache: */ 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; } diff --git a/uip/mhstore.c b/uip/mhstore.c index 518a304..b756956 100644 --- a/uip/mhstore.c +++ b/uip/mhstore.c @@ -251,9 +251,8 @@ do_cache: */ 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; } diff --git a/uip/mhtest.c b/uip/mhtest.c index 9219839..298ef03 100644 --- a/uip/mhtest.c +++ b/uip/mhtest.c @@ -252,9 +252,8 @@ do_cache: */ 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; } diff --git a/uip/msh.c b/uip/msh.c index 0c9c8e0..df64de5 100644 --- a/uip/msh.c +++ b/uip/msh.c @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef HAVE_TERMIOS_H # include @@ -941,9 +942,7 @@ m_gMsgs (int n) 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; diff --git a/uip/packf.c b/uip/packf.c index 9f71b92..6f3638c 100644 --- a/uip/packf.c +++ b/uip/packf.c @@ -119,9 +119,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/pick.c b/uip/pick.c index 4f26bb2..8cdfa58 100644 --- a/uip/pick.c +++ b/uip/pick.c @@ -199,9 +199,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/popsbr.c b/uip/popsbr.c index d1bbafc..9d5f312 100644 --- a/uip/popsbr.c +++ b/uip/popsbr.c @@ -1238,12 +1238,7 @@ sasl_fgetc(FILE *f) } 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; } diff --git a/uip/refile.c b/uip/refile.c index 4d91291..e50253a 100644 --- a/uip/refile.c +++ b/uip/refile.c @@ -185,9 +185,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/replsbr.c b/uip/replsbr.c index d92fade..244fdc9 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -291,11 +291,9 @@ static unsigned int bufsiz=0; /* current size of buf */ 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;\ } diff --git a/uip/rmm.c b/uip/rmm.c index b25f083..b9fd80d 100644 --- a/uip/rmm.c +++ b/uip/rmm.c @@ -98,9 +98,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/scan.c b/uip/scan.c index 3b56830..705ff69 100644 --- a/uip/scan.c +++ b/uip/scan.c @@ -177,9 +177,8 @@ main (int argc, char **argv) */ 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; } diff --git a/uip/sendsbr.c b/uip/sendsbr.c index 81ddaf7..f793b1c 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -360,8 +360,7 @@ get_line(void) } 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; } diff --git a/uip/sortm.c b/uip/sortm.c index 92304dc..5888d78 100644 --- a/uip/sortm.c +++ b/uip/sortm.c @@ -182,9 +182,8 @@ main (int argc, char **argv) */ 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; }