From bfe73fd683053201c326f9ff2c8e5e0f69cc7fe7 Mon Sep 17 00:00:00 2001 From: imp Date: Wed, 16 Sep 1998 04:17:47 +0000 Subject: Replace memory leaking instances of realloc with non-leaking reallocf. In some cases replace if (a == null) a = malloc(x); else a = realloc(a, x); with simple reallocf(a, x). Per ANSI-C, this is guaranteed to be the same thing. I've been running these on my system here w/o ill effects for some time. However, the CTM-express is at part 6 of 34 for the CAM changes, so I've not been able to do a build world with the CAM in the tree with these changes. Shouldn't impact anything, but... --- lib/libc/db/btree/bt_overflow.c | 2 +- lib/libc/db/recno/rec_get.c | 6 +++--- lib/libc/db/recno/rec_put.c | 5 ++--- lib/libc/gen/fts-compat.c | 4 ++-- lib/libc/gen/fts.c | 4 ++-- lib/libc/gen/getcap.c | 14 +++++++------- lib/libc/gen/getcwd.c | 2 +- lib/libc/gen/getgrent.c | 6 +++--- lib/libc/gen/getpwent.c | 2 +- lib/libc/gen/opendir.c | 2 +- lib/libc/gen/scandir.c | 2 +- lib/libc/gen/stringlist.c | 2 +- lib/libc/locale/collate.c | 4 ++-- lib/libc/regex/regcomp.c | 8 ++++---- lib/libc/stdio/asprintf.c | 4 ++-- lib/libc/stdio/fvwrite.c | 4 ++-- lib/libc/stdio/vasprintf.c | 4 ++-- lib/libc/stdio/vfprintf.c | 4 ++-- lib/libc/stdlib/realpath.c | 2 +- lib/libc/stdlib/setenv.c | 2 +- lib/libc/stdtime/localtime.c | 9 +++++++-- lib/libc/stdtime/timelocal.c | 4 ++-- 22 files changed, 50 insertions(+), 46 deletions(-) (limited to 'lib/libc') diff --git a/lib/libc/db/btree/bt_overflow.c b/lib/libc/db/btree/bt_overflow.c index b28b8e0..db28a22 100644 --- a/lib/libc/db/btree/bt_overflow.c +++ b/lib/libc/db/btree/bt_overflow.c @@ -99,7 +99,7 @@ __ovfl_get(t, p, ssz, buf, bufsz) #endif /* Make the buffer bigger as necessary. */ if (*bufsz < sz) { - *buf = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz)); + *buf = (char *)(*buf == NULL ? malloc(sz) : reallocf(*buf, sz)); if (*buf == NULL) return (RET_ERROR); *bufsz = sz; diff --git a/lib/libc/db/recno/rec_get.c b/lib/libc/db/recno/rec_get.c index 47dd773..0a8b880 100644 --- a/lib/libc/db/recno/rec_get.c +++ b/lib/libc/db/recno/rec_get.c @@ -132,7 +132,7 @@ __rec_fpipe(t, top) if (t->bt_rdata.size < t->bt_reclen) { t->bt_rdata.data = t->bt_rdata.data == NULL ? malloc(t->bt_reclen) : - realloc(t->bt_rdata.data, t->bt_reclen); + reallocf(t->bt_rdata.data, t->bt_reclen); if (t->bt_rdata.data == NULL) return (RET_ERROR); t->bt_rdata.size = t->bt_reclen; @@ -205,7 +205,7 @@ __rec_vpipe(t, top) t->bt_rdata.size += (sz = 256); t->bt_rdata.data = t->bt_rdata.data == NULL ? malloc(t->bt_rdata.size) : - realloc(t->bt_rdata.data, t->bt_rdata.size); + reallocf(t->bt_rdata.data, t->bt_rdata.size); if (t->bt_rdata.data == NULL) return (RET_ERROR); p = (u_char *)t->bt_rdata.data + len; @@ -244,7 +244,7 @@ __rec_fmap(t, top) if (t->bt_rdata.size < t->bt_reclen) { t->bt_rdata.data = t->bt_rdata.data == NULL ? malloc(t->bt_reclen) : - realloc(t->bt_rdata.data, t->bt_reclen); + reallocf(t->bt_rdata.data, t->bt_reclen); if (t->bt_rdata.data == NULL) return (RET_ERROR); t->bt_rdata.size = t->bt_reclen; diff --git a/lib/libc/db/recno/rec_put.c b/lib/libc/db/recno/rec_put.c index 1afae0d..0a5bddf 100644 --- a/lib/libc/db/recno/rec_put.c +++ b/lib/libc/db/recno/rec_put.c @@ -88,9 +88,8 @@ __rec_put(dbp, key, data, flags) goto einval; if (t->bt_rdata.size < t->bt_reclen) { - t->bt_rdata.data = t->bt_rdata.data == NULL ? - malloc(t->bt_reclen) : - realloc(t->bt_rdata.data, t->bt_reclen); + t->bt_rdata.data = + reallocf(t->bt_rdata.data, t->bt_reclen); if (t->bt_rdata.data == NULL) return (RET_ERROR); t->bt_rdata.size = t->bt_reclen; diff --git a/lib/libc/gen/fts-compat.c b/lib/libc/gen/fts-compat.c index 02cad6c..da88ed1 100644 --- a/lib/libc/gen/fts-compat.c +++ b/lib/libc/gen/fts-compat.c @@ -881,7 +881,7 @@ fts_sort(sp, head, nitems) */ if (nitems > sp->fts_nitems) { sp->fts_nitems = nitems + 40; - if ((sp->fts_array = realloc(sp->fts_array, + if ((sp->fts_array = reallocf(sp->fts_array, (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) { sp->fts_nitems = 0; return (head); @@ -959,7 +959,7 @@ fts_palloc(sp, more) size_t more; { sp->fts_pathlen += more + 256; - sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen); + sp->fts_path = reallocf(sp->fts_path, (size_t)sp->fts_pathlen); return (sp->fts_path == NULL); } diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c index 02cad6c..da88ed1 100644 --- a/lib/libc/gen/fts.c +++ b/lib/libc/gen/fts.c @@ -881,7 +881,7 @@ fts_sort(sp, head, nitems) */ if (nitems > sp->fts_nitems) { sp->fts_nitems = nitems + 40; - if ((sp->fts_array = realloc(sp->fts_array, + if ((sp->fts_array = reallocf(sp->fts_array, (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) { sp->fts_nitems = 0; return (head); @@ -959,7 +959,7 @@ fts_palloc(sp, more) size_t more; { sp->fts_pathlen += more + 256; - sp->fts_path = realloc(sp->fts_path, (size_t)sp->fts_pathlen); + sp->fts_path = reallocf(sp->fts_path, (size_t)sp->fts_pathlen); return (sp->fts_path == NULL); } diff --git a/lib/libc/gen/getcap.c b/lib/libc/gen/getcap.c index 0295984..aaee0dd 100644 --- a/lib/libc/gen/getcap.c +++ b/lib/libc/gen/getcap.c @@ -344,7 +344,7 @@ getent(cap, len, db_array, fd, name, depth, nfield) pos = rp - record; newsize = r_end - record + BFRAG; - record = realloc(record, newsize); + record = reallocf(record, newsize); if (record == NULL) { errno = ENOMEM; if (myfd) @@ -484,7 +484,7 @@ tc_exp: { newsize = r_end - record + diff + BFRAG; tcpos = tcstart - record; tcposend = tcend - record; - record = realloc(record, newsize); + record = reallocf(record, newsize); if (record == NULL) { errno = ENOMEM; if (myfd) @@ -524,7 +524,7 @@ tc_exp: { *len = rp - record - 1; /* don't count NUL */ if (r_end > rp) if ((record = - realloc(record, (size_t)(rp - record))) == NULL) { + reallocf(record, (size_t)(rp - record))) == NULL) { errno = ENOMEM; return (-2); } @@ -867,7 +867,7 @@ cgetstr(buf, cap, str) if (m_room == 0) { size_t size = mp - mem; - if ((mem = realloc(mem, size + SFRAG)) == NULL) + if ((mem = reallocf(mem, size + SFRAG)) == NULL) return (-2); m_room = SFRAG; mp = mem + size; @@ -881,7 +881,7 @@ cgetstr(buf, cap, str) * Give back any extra memory and return value and success. */ if (m_room != 0) - if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) + if ((mem = reallocf(mem, (size_t)(mp - mem))) == NULL) return (-2); *str = mem; return (len); @@ -940,7 +940,7 @@ cgetustr(buf, cap, str) if (m_room == 0) { size_t size = mp - mem; - if ((mem = realloc(mem, size + SFRAG)) == NULL) + if ((mem = reallocf(mem, size + SFRAG)) == NULL) return (-2); m_room = SFRAG; mp = mem + size; @@ -954,7 +954,7 @@ cgetustr(buf, cap, str) * Give back any extra memory and return value and success. */ if (m_room != 0) - if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) + if ((mem = reallocf(mem, (size_t)(mp - mem))) == NULL) return (-2); *str = mem; return (len); diff --git a/lib/libc/gen/getcwd.c b/lib/libc/gen/getcwd.c index fae252d..8d57e6c 100644 --- a/lib/libc/gen/getcwd.c +++ b/lib/libc/gen/getcwd.c @@ -182,7 +182,7 @@ getcwd(pt, size) * possible component name, plus a trailing NULL. */ if (bup + 3 + MAXNAMLEN + 1 >= eup) { - if ((up = realloc(up, upsize *= 2)) == NULL) + if ((up = reallocf(up, upsize *= 2)) == NULL) goto err; bup = up; eup = up + upsize; diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c index 26069c6..5b5c9da 100644 --- a/lib/libc/gen/getgrent.c +++ b/lib/libc/gen/getgrent.c @@ -266,7 +266,7 @@ grscan(search, gid, name) maxlinelength >= MAXLINELENGTHLIMIT) return(0); - if ((line = (char *)realloc(line, + if ((line = (char *)reallocf(line, sizeof(char) * (maxlinelength + MAXLINELENGTH))) == NULL) return(0); @@ -381,7 +381,7 @@ grscan(search, gid, name) for (m = members; ; bp++) { if (m == (members + maxgrp - 1)) { if ((members = (char **) - realloc(members, + reallocf(members, sizeof(char **) * (maxgrp + MAXGRP))) == NULL) return(0); @@ -439,7 +439,7 @@ _gr_breakout_yp(struct group *gr, char *result) for (m = members; ; s++) { if (m == members + maxgrp - 1) { - if ((members = (char **)realloc(members, + if ((members = (char **)reallocf(members, sizeof(char **) * (maxgrp + MAXGRP))) == NULL) return(0); m = members + maxgrp - 1; diff --git a/lib/libc/gen/getpwent.c b/lib/libc/gen/getpwent.c index b26b927..02ec190 100644 --- a/lib/libc/gen/getpwent.c +++ b/lib/libc/gen/getpwent.c @@ -287,7 +287,7 @@ __hashpw(key) /* Increase buffer size for long lines if necessary. */ if (data.size > max) { max = data.size + 1024; - if (!(line = realloc(line, max))) + if (!(line = reallocf(line, max))) return(0); } diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c index 73da108..ff01646 100644 --- a/lib/libc/gen/opendir.c +++ b/lib/libc/gen/opendir.c @@ -137,7 +137,7 @@ __opendir2(name, flags) if (space < DIRBLKSIZ) { space += incr; len += incr; - buf = realloc(buf, len); + buf = reallocf(buf, len); if (buf == NULL) goto fail; ddptr = buf + (len - space); diff --git a/lib/libc/gen/scandir.c b/lib/libc/gen/scandir.c index e6e8408..8a8b7d2 100644 --- a/lib/libc/gen/scandir.c +++ b/lib/libc/gen/scandir.c @@ -109,7 +109,7 @@ scandir(dirname, namelist, select, dcomp) if (fstat(dirp->dd_fd, &stb) < 0) return(-1); /* just might have grown */ arraysz = stb.st_size / 12; - names = (struct dirent **)realloc((char *)names, + names = (struct dirent **)reallocf((char *)names, arraysz * sizeof(struct dirent *)); if (names == NULL) return(-1); diff --git a/lib/libc/gen/stringlist.c b/lib/libc/gen/stringlist.c index 57c4d91..bb74919 100644 --- a/lib/libc/gen/stringlist.c +++ b/lib/libc/gen/stringlist.c @@ -72,7 +72,7 @@ sl_add(sl, name) { if (sl->sl_cur == sl->sl_max - 1) { sl->sl_max += _SL_CHUNKSIZE; - sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *)); + sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); if (sl->sl_str == NULL) err(1, "stringlist: %m"); } diff --git a/lib/libc/locale/collate.c b/lib/libc/locale/collate.c index d1f03e7..87b4058 100644 --- a/lib/libc/locale/collate.c +++ b/lib/libc/locale/collate.c @@ -24,7 +24,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: collate.c,v 1.15 1997/02/22 14:59:52 peter Exp $ */ #include @@ -114,7 +114,7 @@ __collate_substitute(s) if(!dest_str) dest_str = calloc(dest_len = delta, 1); else - dest_str = realloc(dest_str, dest_len += delta); + dest_str = reallocf(dest_str, dest_len += delta); if(dest_str == NULL) __collate_err(EX_OSERR, __FUNCTION__); } diff --git a/lib/libc/regex/regcomp.c b/lib/libc/regex/regcomp.c index 8bb1355..67fdf47 100644 --- a/lib/libc/regex/regcomp.c +++ b/lib/libc/regex/regcomp.c @@ -1185,12 +1185,12 @@ register struct parse *p; if (p->g->sets == NULL) p->g->sets = (cset *)malloc(nc * sizeof(cset)); else - p->g->sets = (cset *)realloc((char *)p->g->sets, + p->g->sets = (cset *)reallocf((char *)p->g->sets, nc * sizeof(cset)); if (p->g->setbits == NULL) p->g->setbits = (uch *)malloc(nbytes); else { - p->g->setbits = (uch *)realloc((char *)p->g->setbits, + p->g->setbits = (uch *)reallocf((char *)p->g->setbits, nbytes); /* xxx this isn't right if setbits is now NULL */ for (i = 0; i < no; i++) @@ -1331,7 +1331,7 @@ register char *cp; if (cs->multis == NULL) cs->multis = malloc(cs->smultis); else - cs->multis = realloc(cs->multis, cs->smultis); + cs->multis = reallocf(cs->multis, cs->smultis); if (cs->multis == NULL) { SETERROR(REG_ESPACE); return; @@ -1365,7 +1365,7 @@ register char *cp; return; } - cs->multis = realloc(cs->multis, cs->smultis); + cs->multis = reallocf(cs->multis, cs->smultis); assert(cs->multis != NULL); } diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c index 7a3e39a..43e90a9 100644 --- a/lib/libc/stdio/asprintf.c +++ b/lib/libc/stdio/asprintf.c @@ -28,7 +28,7 @@ */ #if defined(LIBC_RCS) && !defined(lint) -static char rcsid[] = "$Id: asprintf.c,v 1.3 1997/02/22 15:01:45 peter Exp $"; +static char rcsid[] = "$Id: asprintf.c,v 1.4 1998/07/08 00:44:51 peter Exp $"; #endif /* LIBC_RCS and not lint */ #include @@ -71,7 +71,7 @@ asprintf(str, fmt, va_alist) ret = vfprintf(&f, fmt, ap); *f._p = '\0'; va_end(ap); - f._bf._base = realloc(f._bf._base, f._bf._size + 1); + f._bf._base = reallocf(f._bf._base, f._bf._size + 1); if (f._bf._base == NULL) { errno = ENOMEM; ret = -1; diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c index 75dfca9..9cce8f2 100644 --- a/lib/libc/stdio/fvwrite.c +++ b/lib/libc/stdio/fvwrite.c @@ -39,7 +39,7 @@ static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93"; #endif static const char rcsid[] = - "$Id: fvwrite.c,v 1.7 1997/12/24 23:23:18 ache Exp $"; + "$Id: fvwrite.c,v 1.8 1998/07/08 00:44:54 peter Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -123,7 +123,7 @@ __sfvwrite(fp, uio) fp->_w = len + 128; fp->_bf._size = blen + len + 128; fp->_bf._base = - realloc(fp->_bf._base, fp->_bf._size + 1); + reallocf(fp->_bf._base, fp->_bf._size + 1); if (fp->_bf._base == NULL) goto err; fp->_p = fp->_bf._base + blen; diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c index 87db4cd..3c509ac 100644 --- a/lib/libc/stdio/vasprintf.c +++ b/lib/libc/stdio/vasprintf.c @@ -28,7 +28,7 @@ */ #if defined(LIBC_RCS) && !defined(lint) -static char rcsid[] = "$Id: vasprintf.c,v 1.8 1998/03/09 06:51:23 jb Exp $"; +static char rcsid[] = "$Id: vasprintf.c,v 1.9 1998/07/08 00:44:56 peter Exp $"; #endif /* LIBC_RCS and not lint */ #include @@ -55,7 +55,7 @@ vasprintf(str, fmt, ap) f._bf._size = f._w = 127; /* Leave room for the NULL */ ret = vfprintf(&f, fmt, ap); *f._p = '\0'; - f._bf._base = realloc(f._bf._base, f._bf._size + 1); + f._bf._base = reallocf(f._bf._base, f._bf._size + 1); if (f._bf._base == NULL) { errno = ENOMEM; ret = -1; diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index 57022e5..3cb58ed 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -39,7 +39,7 @@ static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; #endif static const char rcsid[] = - "$Id: vfprintf.c,v 1.18 1998/04/11 07:40:47 jb Exp $"; + "$Id: vfprintf.c,v 1.19 1998/05/08 05:10:32 jb Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -1182,7 +1182,7 @@ __grow_type_table (nextarg, typetable, tablesize) bcopy (oldtable, *typetable, *tablesize); } else { *typetable = (unsigned char *) - realloc (typetable, sizeof (unsigned char) * newsize); + reallocf (typetable, sizeof (unsigned char) * newsize); } memset (&typetable [*tablesize], T_UNUSED, (newsize - *tablesize)); diff --git a/lib/libc/stdlib/realpath.c b/lib/libc/stdlib/realpath.c index 998ef57..b5177e9 100644 --- a/lib/libc/stdlib/realpath.c +++ b/lib/libc/stdlib/realpath.c @@ -105,7 +105,7 @@ loop: errno = ELOOP; goto err1; } - n = readlink(p, resolved, MAXPATHLEN); + n = readlink(p, resolved, MAXPATHLEN - 1); if (n < 0) goto err1; resolved[n] = '\0'; diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c index d981277..4c00b6e 100644 --- a/lib/libc/stdlib/setenv.c +++ b/lib/libc/stdlib/setenv.c @@ -73,7 +73,7 @@ setenv(name, value, rewrite) for (p = environ, cnt = 0; *p; ++p, ++cnt); if (alloced) { /* just increase size */ - environ = (char **)realloc((char *)environ, + environ = (char **)reallocf((char *)environ, (size_t)(sizeof(char *) * (cnt + 2))); if (!environ) return (-1); diff --git a/lib/libc/stdtime/localtime.c b/lib/libc/stdtime/localtime.c index 7928157..ad227cf 100644 --- a/lib/libc/stdtime/localtime.c +++ b/lib/libc/stdtime/localtime.c @@ -273,6 +273,11 @@ register struct state * const sp; register int i; register int fid; + /* XXX The following is from OpenBSD, and I'm not sure it is correct */ + if (name != NULL && issetugid() != 0) + if ((name[0] == ':' && name[1] == '/') || + name[0] == '/' || strchr(name, '.')) + name = NULL; if (name == NULL && (name = TZDEFAULT) == NULL) return -1; { @@ -293,7 +298,7 @@ register struct state * const sp; if (!doaccess) { if ((p = TZDIR) == NULL) return -1; - if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) + if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname) return -1; (void) strcpy(fullname, p); (void) strcat(fullname, "/"); @@ -306,7 +311,7 @@ register struct state * const sp; name = fullname; } if (doaccess && access(name, R_OK) != 0) - return -1; + return -1; if ((fid = open(name, OPEN_MODE)) == -1) return -1; if ((fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) diff --git a/lib/libc/stdtime/timelocal.c b/lib/libc/stdtime/timelocal.c index ef40035..31f0340 100644 --- a/lib/libc/stdtime/timelocal.c +++ b/lib/libc/stdtime/timelocal.c @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: timelocal.c,v 1.1 1997/08/09 15:43:57 joerg Exp $ */ #include @@ -146,7 +146,7 @@ __time_load_locale(const char *name) bufsize = namesize + st.st_size; locale_buf = NULL; lbuf = (lbuf == NULL || lbuf == locale_buf_C) ? - malloc(bufsize) : realloc(lbuf, bufsize); + malloc(bufsize) : reallocf(lbuf, bufsize); if (lbuf == NULL) goto bad_locale; (void) strcpy(lbuf, name); -- cgit v1.1