From 686afb7f65bfe755e7a023fb45705bdb1415b3ad Mon Sep 17 00:00:00 2001 From: itojun Date: Thu, 16 Oct 2003 06:22:09 +0000 Subject: [PATCH] safer use of realloc --- usr.sbin/lpr/common_source/common.c | 15 ++++++++------- usr.sbin/lpr/lpd/lpd.c | 6 +++--- usr.sbin/makefs/ffs/buf.c | 11 +++++++---- usr.sbin/pppd/pppd/tdb.c | 10 +++++++--- usr.sbin/sup/source/read_line.c | 12 +++++++----- usr.sbin/sup/source/run.c | 11 ++++++++--- usr.sbin/syslogd/syslogd.c | 16 ++++++++++------ 7 files changed, 50 insertions(+), 31 deletions(-) diff --git a/usr.sbin/lpr/common_source/common.c b/usr.sbin/lpr/common_source/common.c index f0cd9fb96289..36a19fe0375a 100644 --- a/usr.sbin/lpr/common_source/common.c +++ b/usr.sbin/lpr/common_source/common.c @@ -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; } diff --git a/usr.sbin/lpr/lpd/lpd.c b/usr.sbin/lpr/lpd/lpd.c index b2a92347b49c..214ea643c5ea 100644 --- a/usr.sbin/lpr/lpd/lpd.c +++ b/usr.sbin/lpr/lpd/lpd.c @@ -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); diff --git a/usr.sbin/makefs/ffs/buf.c b/usr.sbin/makefs/ffs/buf.c index 0a85da8d6114..b4d7f2b87c54 100644 --- a/usr.sbin/makefs/ffs/buf.c +++ b/usr.sbin/makefs/ffs/buf.c @@ -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 #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 @@ -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); diff --git a/usr.sbin/pppd/pppd/tdb.c b/usr.sbin/pppd/pppd/tdb.c index b98ed31f24c7..217f677830be 100644 --- a/usr.sbin/pppd/pppd/tdb.c +++ b/usr.sbin/pppd/pppd/tdb.c @@ -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 #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 @@ -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 */ diff --git a/usr.sbin/sup/source/read_line.c b/usr.sbin/sup/source/read_line.c index 265aadc0948d..0c27503bac1a 100644 --- a/usr.sbin/sup/source/read_line.c +++ b/usr.sbin/sup/source/read_line.c @@ -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 @@ -33,7 +33,7 @@ #include #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 @@ -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'; diff --git a/usr.sbin/sup/source/run.c b/usr.sbin/sup/source/run.c index 7db51aa906ac..d522c3b6e49a 100644 --- a/usr.sbin/sup/source/run.c +++ b/usr.sbin/sup/source/run.c @@ -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 *); } diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 03f5305fe359..c4b72d1c9f43 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -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");