safer use of realloc

This commit is contained in:
itojun 2003-10-16 06:34:19 +00:00
parent 686afb7f65
commit d5250e4ec9
8 changed files with 68 additions and 39 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: apply.c,v 1.11 2003/08/07 11:13:06 agc Exp $ */
/* $NetBSD: apply.c,v 1.12 2003/10/16 06:42:18 itojun Exp $ */
/*-
* Copyright (c) 1994
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
#else
__RCSID("$NetBSD: apply.c,v 1.11 2003/08/07 11:13:06 agc Exp $");
__RCSID("$NetBSD: apply.c,v 1.12 2003/10/16 06:42:18 itojun Exp $");
#endif
#endif /* not lint */
@ -62,7 +62,7 @@ main(argc, argv)
char *argv[];
{
int ch, clen, debug, i, l, magic, n, nargs, rval;
char *c, *cmd, *p, *q;
char *c, *cmd, *p, *q, *nc;
debug = 0;
magic = '%'; /* Default magic char is `%'. */
@ -158,8 +158,13 @@ main(argc, argv)
*/
for (l = strlen(cmd), i = 0; i < nargs; i++)
l += strlen(argv[i+1]);
if (l > clen && (c = realloc(c, clen = l)) == NULL)
err(1, "malloc");
if (l > clen) {
nc = realloc(c, l);
if (nc == NULL)
err(1, "malloc");
c = nc;
clen = l;
}
/* Expand command argv references. */
for (p = cmd, q = c; *p != '\0'; ++p)

View File

@ -1,4 +1,4 @@
/* $NetBSD: cap_mkdb.c,v 1.18 2003/08/07 11:13:15 agc Exp $ */
/* $NetBSD: cap_mkdb.c,v 1.19 2003/10/16 06:43:52 itojun Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
#if 0
static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
#endif
__RCSID("$NetBSD: cap_mkdb.c,v 1.18 2003/08/07 11:13:15 agc Exp $");
__RCSID("$NetBSD: cap_mkdb.c,v 1.19 2003/10/16 06:43:52 itojun Exp $");
#endif /* not lint */
#include <sys/param.h>
@ -171,7 +171,7 @@ db_build(char **ifiles)
recno_t reccnt;
size_t len, bplen;
int st;
char *bp, *p, *t;
char *bp, *p, *t, *n;
data.data = NULL;
key.data = NULL;
@ -183,9 +183,11 @@ db_build(char **ifiles)
*/
len = strlen(bp);
if (bplen <= len + 2) {
bplen += MAX(256, len + 2);
if ((data.data = realloc(data.data, bplen)) == NULL)
if ((n = realloc(data.data,
bplen + MAX(256, len + 2))) == NULL)
err(1, "realloc");
data.data = n;
bplen += MAX(256, len + 2);
}
/* Find the end of the name field. */

View File

@ -1,4 +1,4 @@
/* $NetBSD: soelim.c,v 1.11 2003/08/07 11:15:52 agc Exp $ */
/* $NetBSD: soelim.c,v 1.12 2003/10/16 06:38:11 itojun Exp $ */
/*
* Copyright (c) 1980, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
#if 0
static char sccsid[] = "@(#)soelim.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: soelim.c,v 1.11 2003/08/07 11:15:52 agc Exp $");
__RCSID("$NetBSD: soelim.c,v 1.12 2003/10/16 06:38:11 itojun Exp $");
#endif /* not lint */
/*
@ -90,11 +90,14 @@ initpath(struct path *p)
static void
addpath(struct path *p, const char *dir)
{
char **n;
if (p->list == NULL || p->n <= p->c - 2) {
p->n += 10;
p->list = realloc(p->list, p->n * sizeof(p->list[0]));
if (p->list == NULL)
n = realloc(p->list, (p->n + 10) * sizeof(p->list[0]));
if (n == NULL)
err(1, NULL);
p->list = n;
p->n += 10;
}
if ((p->list[p->c++] = strdup(dir)) == NULL)

View File

@ -1,4 +1,4 @@
/* $NetBSD: read.c,v 1.9 2003/08/07 11:16:02 agc Exp $ */
/* $NetBSD: read.c,v 1.10 2003/10/16 06:39:56 itojun Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: read.c,v 1.9 2003/08/07 11:16:02 agc Exp $");
__RCSID("$NetBSD: read.c,v 1.10 2003/10/16 06:39:56 itojun Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -141,7 +141,7 @@ lines(FILE *fp, __off_t off)
int ch;
char *p;
int blen, cnt, recno, wrap;
char *sp;
char *sp, *n;
p = NULL;
if ((lines = malloc(off * sizeof(*lines))) == NULL)
@ -154,17 +154,20 @@ lines(FILE *fp, __off_t off)
while ((ch = getc(fp)) != EOF) {
if (++cnt > blen) {
if ((sp = realloc(sp, blen += 1024)) == NULL)
if ((n = realloc(sp, blen + 1024)) == NULL)
err(1, "%s", strerror(errno));
sp = n;
blen += 1024;
p = sp + cnt - 1;
}
*p++ = ch;
if (ch == '\n') {
if (lines[recno].blen < cnt) {
lines[recno].blen = cnt + 256;
if ((lines[recno].l = realloc(lines[recno].l,
lines[recno].blen)) == NULL)
if ((n = realloc(lines[recno].l,
cnt + 256)) == NULL)
err(1, "%s", strerror(errno));
lines[recno].l = n;
lines[recno].blen = cnt + 256;
}
memmove(lines[recno].l, sp, lines[recno].len = cnt);
cnt = 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: m_netbsd15.c,v 1.20 2003/10/03 15:32:06 christos Exp $ */
/* $NetBSD: m_netbsd15.c,v 1.21 2003/10/16 06:34:19 itojun Exp $ */
/*
* top - a top users display for Unix
@ -36,12 +36,12 @@
* Tomas Svensson <ts@unix1.net>
*
*
* $Id: m_netbsd15.c,v 1.20 2003/10/03 15:32:06 christos Exp $
* $Id: m_netbsd15.c,v 1.21 2003/10/16 06:34:19 itojun Exp $
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: m_netbsd15.c,v 1.20 2003/10/03 15:32:06 christos Exp $");
__RCSID("$NetBSD: m_netbsd15.c,v 1.21 2003/10/16 06:34:19 itojun Exp $");
#endif
#include <sys/param.h>
@ -423,7 +423,7 @@ get_process_info(si, sel, compare)
int i;
int total_procs;
int active_procs;
struct kinfo_proc2 **prefp;
struct kinfo_proc2 **prefp, **n;
struct kinfo_proc2 *pp;
/* these are copied out of sel for speed */
@ -436,13 +436,20 @@ get_process_info(si, sel, compare)
pbase = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
if (nproc > onproc)
pref = (struct kinfo_proc2 **) realloc(pref,
sizeof(struct kinfo_proc2 *) * (onproc = nproc));
if (pref == NULL || pbase == NULL) {
if (pbase == NULL) {
(void) fprintf(stderr, "top: Out of memory.\n");
quit(23);
}
if (nproc > onproc) {
n = (struct kinfo_proc2 **) realloc(pref,
sizeof(struct kinfo_proc2 *) * nproc);
if (n == NULL) {
(void) fprintf(stderr, "top: Out of memory.\n");
quit(23);
}
pref = n;
onproc = nproc;
}
/* get a pointer to the states summary array */
si->procstates = process_states;

View File

@ -1,4 +1,4 @@
/* $NetBSD: tsort.c,v 1.18 2003/08/07 11:16:50 agc Exp $ */
/* $NetBSD: tsort.c,v 1.19 2003/10/16 06:37:22 itojun Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)tsort.c 8.3 (Berkeley) 5/4/95";
#endif
__RCSID("$NetBSD: tsort.c,v 1.18 2003/08/07 11:16:50 agc Exp $");
__RCSID("$NetBSD: tsort.c,v 1.19 2003/10/16 06:37:22 itojun Exp $");
#endif /* not lint */
#if HAVE_CONFIG_H
@ -199,8 +199,11 @@ grow_buf(bp, size)
void *bp;
int size;
{
if ((bp = realloc(bp, (u_int)size)) == NULL)
void *n;
if ((n = realloc(bp, (u_int)size)) == NULL)
err(1, "realloc");
bp = n;
return (bp);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: whois.c,v 1.24 2003/10/11 09:06:03 wiz Exp $ */
/* $NetBSD: whois.c,v 1.25 2003/10/16 06:36:51 itojun Exp $ */
/* $OpenBSD: whois.c,v 1.28 2003/09/18 22:16:15 fgsch Exp $ */
/*
@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
#if 0
static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: whois.c,v 1.24 2003/10/11 09:06:03 wiz Exp $");
__RCSID("$NetBSD: whois.c,v 1.25 2003/10/16 06:36:51 itojun Exp $");
#endif
#endif /* not lint */
@ -280,6 +280,7 @@ static char *
choose_server(const char *name, const char *country)
{
static char *server;
char *nserver;
const char *qhead;
char *ep;
size_t len;
@ -298,8 +299,9 @@ choose_server(const char *name, const char *country)
} else if (isdigit((unsigned char)*(++qhead)))
return (ANICHOST);
len = strlen(qhead) + sizeof(QNICHOST_TAIL);
if ((server = realloc(server, len)) == NULL)
if ((nserver = realloc(server, len)) == NULL)
err(1, "realloc");
server = nserver;
(void)strlcpy(server, qhead, len);
(void)strlcat(server, QNICHOST_TAIL, len);
return (server);

View File

@ -1,4 +1,4 @@
/* $NetBSD: mem.c,v 1.3 2002/01/31 19:36:53 tv Exp $ */
/* $NetBSD: mem.c,v 1.4 2003/10/16 06:35:26 itojun Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: mem.c,v 1.3 2002/01/31 19:36:53 tv Exp $");
__RCSID("$NetBSD: mem.c,v 1.4 2003/10/16 06:35:26 itojun Exp $");
#endif
#include <stdlib.h>
@ -64,9 +64,13 @@ xcalloc(size_t n, size_t s)
void *
xrealloc(void *p, size_t s)
{
void *n;
if ((p = realloc(p, s)) == NULL)
if ((n = realloc(p, s)) == NULL) {
free(p);
nomem();
}
p = n;
return (p);
}