safer use of realloc
This commit is contained in:
parent
686afb7f65
commit
d5250e4ec9
@ -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
|
* Copyright (c) 1994
|
||||||
@ -37,7 +37,7 @@
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
|
static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
|
||||||
#else
|
#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
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ main(argc, argv)
|
|||||||
char *argv[];
|
char *argv[];
|
||||||
{
|
{
|
||||||
int ch, clen, debug, i, l, magic, n, nargs, rval;
|
int ch, clen, debug, i, l, magic, n, nargs, rval;
|
||||||
char *c, *cmd, *p, *q;
|
char *c, *cmd, *p, *q, *nc;
|
||||||
|
|
||||||
debug = 0;
|
debug = 0;
|
||||||
magic = '%'; /* Default magic char is `%'. */
|
magic = '%'; /* Default magic char is `%'. */
|
||||||
@ -158,8 +158,13 @@ main(argc, argv)
|
|||||||
*/
|
*/
|
||||||
for (l = strlen(cmd), i = 0; i < nargs; i++)
|
for (l = strlen(cmd), i = 0; i < nargs; i++)
|
||||||
l += strlen(argv[i+1]);
|
l += strlen(argv[i+1]);
|
||||||
if (l > clen && (c = realloc(c, clen = l)) == NULL)
|
if (l > clen) {
|
||||||
err(1, "malloc");
|
nc = realloc(c, l);
|
||||||
|
if (nc == NULL)
|
||||||
|
err(1, "malloc");
|
||||||
|
c = nc;
|
||||||
|
clen = l;
|
||||||
|
}
|
||||||
|
|
||||||
/* Expand command argv references. */
|
/* Expand command argv references. */
|
||||||
for (p = cmd, q = c; *p != '\0'; ++p)
|
for (p = cmd, q = c; *p != '\0'; ++p)
|
||||||
|
@ -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
|
* Copyright (c) 1992, 1993
|
||||||
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
|
static char sccsid[] = "@(#)cap_mkdb.c 8.2 (Berkeley) 4/27/95";
|
||||||
#endif
|
#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 */
|
#endif /* not lint */
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
@ -171,7 +171,7 @@ db_build(char **ifiles)
|
|||||||
recno_t reccnt;
|
recno_t reccnt;
|
||||||
size_t len, bplen;
|
size_t len, bplen;
|
||||||
int st;
|
int st;
|
||||||
char *bp, *p, *t;
|
char *bp, *p, *t, *n;
|
||||||
|
|
||||||
data.data = NULL;
|
data.data = NULL;
|
||||||
key.data = NULL;
|
key.data = NULL;
|
||||||
@ -183,9 +183,11 @@ db_build(char **ifiles)
|
|||||||
*/
|
*/
|
||||||
len = strlen(bp);
|
len = strlen(bp);
|
||||||
if (bplen <= len + 2) {
|
if (bplen <= len + 2) {
|
||||||
bplen += MAX(256, len + 2);
|
if ((n = realloc(data.data,
|
||||||
if ((data.data = realloc(data.data, bplen)) == NULL)
|
bplen + MAX(256, len + 2))) == NULL)
|
||||||
err(1, "realloc");
|
err(1, "realloc");
|
||||||
|
data.data = n;
|
||||||
|
bplen += MAX(256, len + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find the end of the name field. */
|
/* Find the end of the name field. */
|
||||||
|
@ -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
|
* Copyright (c) 1980, 1993
|
||||||
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)soelim.c 8.1 (Berkeley) 6/6/93";
|
static char sccsid[] = "@(#)soelim.c 8.1 (Berkeley) 6/6/93";
|
||||||
#endif
|
#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 */
|
#endif /* not lint */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -90,11 +90,14 @@ initpath(struct path *p)
|
|||||||
static void
|
static void
|
||||||
addpath(struct path *p, const char *dir)
|
addpath(struct path *p, const char *dir)
|
||||||
{
|
{
|
||||||
|
char **n;
|
||||||
|
|
||||||
if (p->list == NULL || p->n <= p->c - 2) {
|
if (p->list == NULL || p->n <= p->c - 2) {
|
||||||
p->n += 10;
|
n = realloc(p->list, (p->n + 10) * sizeof(p->list[0]));
|
||||||
p->list = realloc(p->list, p->n * sizeof(p->list[0]));
|
if (n == NULL)
|
||||||
if (p->list == NULL)
|
|
||||||
err(1, NULL);
|
err(1, NULL);
|
||||||
|
p->list = n;
|
||||||
|
p->n += 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((p->list[p->c++] = strdup(dir)) == NULL)
|
if ((p->list[p->c++] = strdup(dir)) == NULL)
|
||||||
|
@ -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
|
* Copyright (c) 1991, 1993
|
||||||
@ -37,7 +37,7 @@
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/6/93";
|
static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/6/93";
|
||||||
#endif
|
#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 */
|
#endif /* not lint */
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -141,7 +141,7 @@ lines(FILE *fp, __off_t off)
|
|||||||
int ch;
|
int ch;
|
||||||
char *p;
|
char *p;
|
||||||
int blen, cnt, recno, wrap;
|
int blen, cnt, recno, wrap;
|
||||||
char *sp;
|
char *sp, *n;
|
||||||
|
|
||||||
p = NULL;
|
p = NULL;
|
||||||
if ((lines = malloc(off * sizeof(*lines))) == NULL)
|
if ((lines = malloc(off * sizeof(*lines))) == NULL)
|
||||||
@ -154,17 +154,20 @@ lines(FILE *fp, __off_t off)
|
|||||||
|
|
||||||
while ((ch = getc(fp)) != EOF) {
|
while ((ch = getc(fp)) != EOF) {
|
||||||
if (++cnt > blen) {
|
if (++cnt > blen) {
|
||||||
if ((sp = realloc(sp, blen += 1024)) == NULL)
|
if ((n = realloc(sp, blen + 1024)) == NULL)
|
||||||
err(1, "%s", strerror(errno));
|
err(1, "%s", strerror(errno));
|
||||||
|
sp = n;
|
||||||
|
blen += 1024;
|
||||||
p = sp + cnt - 1;
|
p = sp + cnt - 1;
|
||||||
}
|
}
|
||||||
*p++ = ch;
|
*p++ = ch;
|
||||||
if (ch == '\n') {
|
if (ch == '\n') {
|
||||||
if (lines[recno].blen < cnt) {
|
if (lines[recno].blen < cnt) {
|
||||||
lines[recno].blen = cnt + 256;
|
if ((n = realloc(lines[recno].l,
|
||||||
if ((lines[recno].l = realloc(lines[recno].l,
|
cnt + 256)) == NULL)
|
||||||
lines[recno].blen)) == NULL)
|
|
||||||
err(1, "%s", strerror(errno));
|
err(1, "%s", strerror(errno));
|
||||||
|
lines[recno].l = n;
|
||||||
|
lines[recno].blen = cnt + 256;
|
||||||
}
|
}
|
||||||
memmove(lines[recno].l, sp, lines[recno].len = cnt);
|
memmove(lines[recno].l, sp, lines[recno].len = cnt);
|
||||||
cnt = 0;
|
cnt = 0;
|
||||||
|
@ -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
|
* top - a top users display for Unix
|
||||||
@ -36,12 +36,12 @@
|
|||||||
* Tomas Svensson <ts@unix1.net>
|
* 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>
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
#ifndef lint
|
#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
|
#endif
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
@ -423,7 +423,7 @@ get_process_info(si, sel, compare)
|
|||||||
int i;
|
int i;
|
||||||
int total_procs;
|
int total_procs;
|
||||||
int active_procs;
|
int active_procs;
|
||||||
struct kinfo_proc2 **prefp;
|
struct kinfo_proc2 **prefp, **n;
|
||||||
struct kinfo_proc2 *pp;
|
struct kinfo_proc2 *pp;
|
||||||
|
|
||||||
/* these are copied out of sel for speed */
|
/* 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);
|
pbase = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
|
||||||
if (nproc > onproc)
|
if (pbase == NULL) {
|
||||||
pref = (struct kinfo_proc2 **) realloc(pref,
|
|
||||||
sizeof(struct kinfo_proc2 *) * (onproc = nproc));
|
|
||||||
if (pref == NULL || pbase == NULL) {
|
|
||||||
(void) fprintf(stderr, "top: Out of memory.\n");
|
(void) fprintf(stderr, "top: Out of memory.\n");
|
||||||
quit(23);
|
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 */
|
/* get a pointer to the states summary array */
|
||||||
si->procstates = process_states;
|
si->procstates = process_states;
|
||||||
|
|
||||||
|
@ -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
|
* Copyright (c) 1989, 1993, 1994
|
||||||
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
|
|||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)tsort.c 8.3 (Berkeley) 5/4/95";
|
static char sccsid[] = "@(#)tsort.c 8.3 (Berkeley) 5/4/95";
|
||||||
#endif
|
#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 */
|
#endif /* not lint */
|
||||||
|
|
||||||
#if HAVE_CONFIG_H
|
#if HAVE_CONFIG_H
|
||||||
@ -199,8 +199,11 @@ grow_buf(bp, size)
|
|||||||
void *bp;
|
void *bp;
|
||||||
int size;
|
int size;
|
||||||
{
|
{
|
||||||
if ((bp = realloc(bp, (u_int)size)) == NULL)
|
void *n;
|
||||||
|
|
||||||
|
if ((n = realloc(bp, (u_int)size)) == NULL)
|
||||||
err(1, "realloc");
|
err(1, "realloc");
|
||||||
|
bp = n;
|
||||||
return (bp);
|
return (bp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 $ */
|
/* $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
|
#if 0
|
||||||
static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
|
static const char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
|
||||||
#else
|
#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
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
@ -280,6 +280,7 @@ static char *
|
|||||||
choose_server(const char *name, const char *country)
|
choose_server(const char *name, const char *country)
|
||||||
{
|
{
|
||||||
static char *server;
|
static char *server;
|
||||||
|
char *nserver;
|
||||||
const char *qhead;
|
const char *qhead;
|
||||||
char *ep;
|
char *ep;
|
||||||
size_t len;
|
size_t len;
|
||||||
@ -298,8 +299,9 @@ choose_server(const char *name, const char *country)
|
|||||||
} else if (isdigit((unsigned char)*(++qhead)))
|
} else if (isdigit((unsigned char)*(++qhead)))
|
||||||
return (ANICHOST);
|
return (ANICHOST);
|
||||||
len = strlen(qhead) + sizeof(QNICHOST_TAIL);
|
len = strlen(qhead) + sizeof(QNICHOST_TAIL);
|
||||||
if ((server = realloc(server, len)) == NULL)
|
if ((nserver = realloc(server, len)) == NULL)
|
||||||
err(1, "realloc");
|
err(1, "realloc");
|
||||||
|
server = nserver;
|
||||||
(void)strlcpy(server, qhead, len);
|
(void)strlcpy(server, qhead, len);
|
||||||
(void)strlcat(server, QNICHOST_TAIL, len);
|
(void)strlcat(server, QNICHOST_TAIL, len);
|
||||||
return (server);
|
return (server);
|
||||||
|
@ -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
|
* Copyright (c) 1994, 1995 Jochen Pohl
|
||||||
@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#if defined(__RCSID) && !defined(lint)
|
#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
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -64,9 +64,13 @@ xcalloc(size_t n, size_t s)
|
|||||||
void *
|
void *
|
||||||
xrealloc(void *p, size_t s)
|
xrealloc(void *p, size_t s)
|
||||||
{
|
{
|
||||||
|
void *n;
|
||||||
|
|
||||||
if ((p = realloc(p, s)) == NULL)
|
if ((n = realloc(p, s)) == NULL) {
|
||||||
|
free(p);
|
||||||
nomem();
|
nomem();
|
||||||
|
}
|
||||||
|
p = n;
|
||||||
return (p);
|
return (p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user