safer use of realloc

This commit is contained in:
itojun 2003-10-16 06:22:09 +00:00
parent 33c55cb2ac
commit 686afb7f65
7 changed files with 50 additions and 31 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: common.c,v 1.25 2003/08/07 11:25:24 agc Exp $ */
/* $NetBSD: common.c,v 1.26 2003/10/16 06:30:11 itojun Exp $ */
/*
* Copyright (c) 1983, 1993
@ -39,7 +39,7 @@
#if 0
static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: common.c,v 1.25 2003/08/07 11:25:24 agc Exp $");
__RCSID("$NetBSD: common.c,v 1.26 2003/10/16 06:30:11 itojun Exp $");
#endif
#endif /* not lint */
@ -221,7 +221,7 @@ int
getq(struct queue **namelist[])
{
struct dirent *d;
struct queue *q, **queue;
struct queue *q, **queue, **nqueue;
struct stat stbuf;
DIR *dirp;
u_int nitems, arraysz;
@ -263,11 +263,12 @@ getq(struct queue **namelist[])
* realloc the maximum size.
*/
if (++nitems > arraysz) {
arraysz *= 2;
queue = (struct queue **)realloc(queue,
arraysz * sizeof(struct queue *));
if (queue == NULL)
nqueue = (struct queue **)realloc(queue,
arraysz * 2 * sizeof(struct queue *));
if (nqueue == NULL)
goto errdone;
queue = nqueue;
arraysz *= 2;
}
queue[nitems-1] = q;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lpd.c,v 1.48 2003/10/16 03:03:04 itojun Exp $ */
/* $NetBSD: lpd.c,v 1.49 2003/10/16 06:30:11 itojun Exp $ */
/*
* Copyright (c) 1983, 1993, 1994
@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)lpd.c 8.7 (Berkeley) 5/10/95";
#else
__RCSID("$NetBSD: lpd.c,v 1.48 2003/10/16 03:03:04 itojun Exp $");
__RCSID("$NetBSD: lpd.c,v 1.49 2003/10/16 06:30:11 itojun Exp $");
#endif
#endif /* not lint */
@ -763,7 +763,7 @@ socksetup(int af, int options, const char *port, int *nfds)
/* Count max number of sockets we may open */
for (r = res, n = 0; r; r = r->ai_next, n++)
;
newsocks= realloc(socks, (*nfds + n) * sizeof(socks[0]));
newsocks = realloc(socks, (*nfds + n) * sizeof(socks[0]));
if (!newsocks) {
syslog(LOG_ERR, "couldn't allocate memory for sockets");
mcleanup(0);

View File

@ -1,4 +1,4 @@
/* $NetBSD: buf.c,v 1.10 2003/01/24 21:55:32 fvdl Exp $ */
/* $NetBSD: buf.c,v 1.11 2003/10/16 06:31:38 itojun Exp $ */
/*
* Copyright (c) 2001 Wasabi Systems, Inc.
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
__RCSID("$NetBSD: buf.c,v 1.10 2003/01/24 21:55:32 fvdl Exp $");
__RCSID("$NetBSD: buf.c,v 1.11 2003/10/16 06:31:38 itojun Exp $");
#endif /* !__lint */
#include <sys/param.h>
@ -180,6 +180,7 @@ getblk(int fd, struct fs *fs, daddr_t blkno, int size)
{
static int buftailinitted;
struct buf *bp;
void *n;
assert (fs != NULL);
if (debug & DEBUG_BUF_GETBLK)
@ -212,9 +213,11 @@ getblk(int fd, struct fs *fs, daddr_t blkno, int size)
}
bp->b_bcount = size;
if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
bp->b_bufsize = size;
if ((bp->b_data = realloc(bp->b_data, bp->b_bufsize)) == NULL)
n = realloc(bp->b_data, size);
if (n == NULL)
err(1, "getblk: realloc b_data %ld", bp->b_bcount);
bp->b_data = n;
bp->b_bufsize = size;
}
return (bp);

View File

@ -1,4 +1,4 @@
/* $NetBSD: tdb.c,v 1.3 2002/05/29 19:06:33 christos Exp $ */
/* $NetBSD: tdb.c,v 1.4 2003/10/16 06:28:47 itojun Exp $ */
/*
* Database functions
@ -21,7 +21,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: tdb.c,v 1.3 2002/05/29 19:06:33 christos Exp $");
__RCSID("$NetBSD: tdb.c,v 1.4 2003/10/16 06:28:47 itojun Exp $");
#endif
#include <stdlib.h>
@ -369,7 +369,11 @@ static int tdb_expand(TDB_CONTEXT *tdb, tdb_off length)
tdb->map_size += length;
if (tdb->fd == -1) {
tdb->map_ptr = realloc(tdb->map_ptr, tdb->map_size);
void *n;
n = realloc(tdb->map_ptr, tdb->map_size);
if (!n)
goto fail;
tdb->map_ptr = n;
}
/* write it out */

View File

@ -1,4 +1,4 @@
/* $NetBSD: read_line.c,v 1.7 2003/04/04 23:10:10 christos Exp $ */
/* $NetBSD: read_line.c,v 1.8 2003/10/16 06:26:06 itojun Exp $ */
/*
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
#if defined(lint) && defined(__RCSID)
__RCSID("$NetBSD: read_line.c,v 1.7 2003/04/04 23:10:10 christos Exp $");
__RCSID("$NetBSD: read_line.c,v 1.8 2003/10/16 06:26:06 itojun Exp $");
#endif
#include <sys/param.h>
@ -64,6 +64,7 @@ read_line(FILE * fp, size_t * size, size_t * lineno, const char *delim,
free(buf);
return (buf = fparseln(fp, size, lineno, delim, flags));
#else
char *n;
#ifndef HAS_FGETLN
char sbuf[1024];
#endif
@ -117,11 +118,12 @@ read_line(FILE * fp, size_t * size, size_t * lineno, const char *delim,
}
if (len + s + 1 > buflen) {
n = realloc(buf, len + s + 1);
if (n == NULL)
err(1, "can't realloc");
buf = n;
buflen = len + s + 1;
buf = realloc(buf, buflen);
}
if (buf == NULL)
err(1, "can't realloc");
memcpy(buf + len, ptr, s);
len += s;
buf[len] = '\0';

View File

@ -1,4 +1,4 @@
/* $NetBSD: run.c,v 1.11 2002/07/10 20:19:42 wiz Exp $ */
/* $NetBSD: run.c,v 1.12 2003/10/16 06:26:06 itojun Exp $ */
/*
* Copyright (c) 1991 Carnegie Mellon University
@ -103,13 +103,18 @@ makearglist(va_list ap)
{
static size_t ns = 0;
static char **np = NULL;
char **nnp;
int i = 0;
do {
if (i >= ns) {
ns += 20;
if ((np = realloc(np, ns)) == NULL)
nnp = realloc(np, ns + 20);
if (nnp == NULL) {
free(np);
return NULL;
}
np = nnp;
ns += 20;
}
np[i] = va_arg(ap, char *);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: syslogd.c,v 1.61 2003/09/19 08:24:48 itojun Exp $ */
/* $NetBSD: syslogd.c,v 1.62 2003/10/16 06:22:09 itojun Exp $ */
/*
* Copyright (c) 1983, 1988, 1993, 1994
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94";
#else
__RCSID("$NetBSD: syslogd.c,v 1.61 2003/09/19 08:24:48 itojun Exp $");
__RCSID("$NetBSD: syslogd.c,v 1.62 2003/10/16 06:22:09 itojun Exp $");
#endif
#endif /* not lint */
@ -568,19 +568,23 @@ usage(void)
void
logpath_add(char ***lp, int *szp, int *maxszp, char *new)
{
char **nlp;
int newmaxsz;
dprintf("Adding `%s' to the %p logpath list\n", new, *lp);
if (*szp == *maxszp) {
if (*maxszp == 0) {
*maxszp = 4; /* start of with enough for now */
newmaxsz = 4; /* start of with enough for now */
*lp = NULL;
} else
*maxszp *= 2;
*lp = realloc(*lp, sizeof(char *) * (*maxszp + 1));
if (*lp == NULL) {
newmaxsz = *maxszp * 2;
nlp = realloc(*lp, sizeof(char *) * (newmaxsz + 1));
if (nlp == NULL) {
logerror("Couldn't allocate line buffer");
die(0);
}
*lp = nlp;
*maxszp = newmaxsz;
}
if (((*lp)[(*szp)++] = strdup(new)) == NULL) {
logerror("Couldn't allocate logpath");