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...
This commit is contained in:
Warner Losh 1998-09-16 04:17:47 +00:00
parent e936391783
commit e8420087b0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=39327
34 changed files with 70 additions and 67 deletions

View File

@ -408,7 +408,7 @@ l_CHAR(Void)
*p++ = ch;
f__lcount = 10*f__lcount + ch - '0';
if (++i == size) {
f__lchar = (char *)realloc(f__lchar,
f__lchar = (char *)reallocf(f__lchar,
(unsigned int)(size += BUFSIZE));
if(f__lchar == NULL)
errfl(f__elist->cierr,113,rafail);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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");
}

View File

@ -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 <rune.h>
@ -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__);
}

View File

@ -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);
}

View File

@ -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 <stdio.h>
@ -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;

View File

@ -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 <stdio.h>
@ -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;

View File

@ -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 <stdio.h>
@ -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;

View File

@ -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));

View File

@ -105,7 +105,7 @@ realpath(path, resolved)
errno = ELOOP;
goto err1;
}
n = readlink(p, resolved, MAXPATHLEN);
n = readlink(p, resolved, MAXPATHLEN - 1);
if (n < 0)
goto err1;
resolved[n] = '\0';

View File

@ -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);

View File

@ -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))

View File

@ -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 <sys/types.h>
@ -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);

View File

@ -77,7 +77,7 @@ el_init(prog, fin, fout)
el->el_prog = strdup(prog);
#ifdef DEBUG
if ((tty = getenv("DEBUGTTY")) != NULL) {
if (issetugid() == 0 && (tty = getenv("DEBUGTTY")) != NULL) {
el->el_errfile = fopen(tty, "w");
if (el->el_errfile == NULL) {
extern errno;
@ -291,7 +291,7 @@ el_source(el, fname)
if (fname == NULL) {
fname = &elpath[1];
if ((fp = fopen(fname, "r")) == NULL) {
if ((ptr = getenv("HOME")) == NULL)
if (issetugid() != 0 || (ptr = getenv("HOME")) == NULL)
return -1;
(void)snprintf(path, sizeof(path), "%s%s", ptr, elpath);
fname = path;

View File

@ -86,6 +86,7 @@ typedef struct el_state_t {
*/
#define el_malloc(a) malloc(a)
#define el_realloc(a,b) realloc(a, b)
#define el_reallocf(a,b) reallocf(a, b)
#define el_free(a) free(a)
#include "tty.h"

View File

@ -1380,9 +1380,9 @@ map_addfunc(el, name, help, func)
return -1;
el->el_map.func = (el_func_t *)
el_realloc(el->el_map.func, nf * sizeof(el_func_t));
el_reallocf(el->el_map.func, nf * sizeof(el_func_t));
el->el_map.help = (el_bindings_t *)
el_realloc(el->el_map.help, nf * sizeof(el_bindings_t));
el_reallocf(el->el_map.help, nf * sizeof(el_bindings_t));
nf = el->el_map.nfunc;
el->el_map.func[nf] = func;

View File

@ -59,6 +59,7 @@ typedef enum { Q_none, Q_single, Q_double, Q_one, Q_doubleone } quote_t;
#define tok_malloc(a) malloc(a)
#define tok_free(a) free(a)
#define tok_realloc(a, b) realloc(a, b)
#define tok_reallocf(a, b) reallocf(a, b)
struct tokenizer {
@ -377,8 +378,8 @@ tok_line(tok, line, argc, argv)
if (tok->argc >= tok->amax - 4) {
tok->amax += AINCR;
tok->argv = (char **) tok_realloc(tok->argv,
tok->amax * sizeof(char*));
tok->argv = (char **) tok_reallocf(tok->argv,
tok->amax * sizeof(char*));
}
}

View File

@ -370,8 +370,10 @@ _kvm_realloc(kd, p, n)
{
void *np = (void *)realloc(p, n);
if (np == 0)
if (np == 0) {
free(p);
_kvm_err(kd, kd->program, "out of memory");
}
return (np);
}

View File

@ -57,13 +57,8 @@ int type; {
if (files >= size) {
size += HUNK;
if (path == NULL)
path = (struct term_path *)
malloc(size * sizeof(struct term_path));
else
path = (struct term_path *)
realloc((anyptr) path,
size * sizeof(struct term_path));
path = (struct term_path *) reallocf((anyptr) path,
size * sizeof(struct term_path));
if (path == NULL)
return 0;
}

View File

@ -111,7 +111,7 @@ void ss_add_info_dir(sci_idx, info_dir, code_ptr)
dirs = info->info_dirs;
for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
; /* get number of non-NULL dir entries */
dirs = (char **)realloc((char *)dirs,
dirs = (char **)reallocf((char *)dirs,
(unsigned)(n_dirs + 2)*sizeof(char *));
if (dirs == (char **)NULL) {
info->info_dirs = (char **)NULL;

View File

@ -37,7 +37,7 @@ int ss_create_invocation(subsystem_name, version_string, info_ptr,
for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
;
table = (ss_data **) realloc((char *)table,
table = (ss_data **) reallocf((char *)table,
((unsigned)sci_idx+2)*size);
table[sci_idx+1] = (ss_data *) NULL;
table[sci_idx] = new_table;

View File

@ -31,7 +31,7 @@ enum parse_mode { WHITESPACE, TOKEN, QUOTED_STRING };
* Series of pointers to parsed tokens.
*/
#define NEW_ARGV(old,n) (char **)realloc((char *)old,\
#define NEW_ARGV(old,n) (char **)reallocf((char *)old,\
(unsigned)(n+2)*sizeof(char*))
char **ss_parse (sci_idx, line_ptr, argc_ptr)

View File

@ -25,7 +25,7 @@ ss_add_request_table(sci_idx, rqtbl_ptr, position, code_ptr)
;
/* size == C subscript of NULL == #elements */
size += 2; /* new element, and NULL */
info->rqt_tables = (ssrt **)realloc((char *)info->rqt_tables,
info->rqt_tables = (ssrt **)reallocf((char *)info->rqt_tables,
(unsigned)size*sizeof(ssrt));
if (info->rqt_tables == (ssrt **)NULL) {
*code_ptr = errno;

View File

@ -25,7 +25,7 @@
*
* Low-level routines relating to the user capabilities database
*
* $Id: login_auth.c,v 1.7 1997/05/10 18:55:37 davidn Exp $
* $Id: login_auth.c,v 1.8 1997/07/19 04:47:05 davidn Exp $
*/
#include <sys/types.h>
@ -393,7 +393,7 @@ auth_mkvalue(const char *value)
}
}
*p = '\0';
big = realloc(big, strlen(big) + 1);
big = reallocf(big, strlen(big) + 1);
}
return big;
}