From e99b62a1d717735f4180bbc1449e5af0fcb7977e Mon Sep 17 00:00:00 2001 From: itojun Date: Fri, 19 Sep 2003 06:11:35 +0000 Subject: [PATCH] realloc pedant --- usr.sbin/ac/ac.c | 12 ++++++++---- usr.sbin/config/util.c | 6 ++++-- usr.sbin/makefs/ffs.c | 12 +++++++----- usr.sbin/makefs/walk.c | 11 ++++++----- usr.sbin/mtree/spec.c | 11 ++++++----- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/usr.sbin/ac/ac.c b/usr.sbin/ac/ac.c index a27121f1cb18..c1b425e78ab6 100644 --- a/usr.sbin/ac/ac.c +++ b/usr.sbin/ac/ac.c @@ -1,4 +1,4 @@ -/* $NetBSD: ac.c,v 1.15 2003/05/17 18:55:18 itojun Exp $ */ +/* $NetBSD: ac.c,v 1.16 2003/09/19 06:19:02 itojun Exp $ */ /* * Copyright (c) 1994 Christopher G. Demetriou @@ -49,7 +49,7 @@ #include #ifndef lint -__RCSID("$NetBSD: ac.c,v 1.15 2003/05/17 18:55:18 itojun Exp $"); +__RCSID("$NetBSD: ac.c,v 1.16 2003/09/19 06:19:02 itojun Exp $"); #endif #include @@ -224,6 +224,7 @@ static void find_login_ttys() { struct ttyent *tty; + char (*nCon)[UT_LINESIZE]; if ((Con = malloc((Maxcon = 10) * sizeof(Con[0]))) == NULL) err(1, "malloc"); @@ -232,10 +233,13 @@ find_login_ttys() while ((tty = getttyent()) != NULL) if ((tty->ty_status & TTY_ON) != 0 && strstr(tty->ty_getty, "getty") != NULL) { - if (Ncon == Maxcon) - if ((Con = realloc(Con, (Maxcon += 10) * + if (Ncon == Maxcon) { + if ((nCon = realloc(Con, (Maxcon + 10) * sizeof(Con[0]))) == NULL) err(1, "malloc"); + Con = nCon; + Maxcon += 10; + } (void)strncpy(Con[Ncon++], tty->ty_name, UT_LINESIZE); } endttyent(); diff --git a/usr.sbin/config/util.c b/usr.sbin/config/util.c index ae6b9c827f0f..169bec54f70e 100644 --- a/usr.sbin/config/util.c +++ b/usr.sbin/config/util.c @@ -1,4 +1,4 @@ -/* $NetBSD: util.c,v 1.17 2003/08/07 11:25:17 agc Exp $ */ +/* $NetBSD: util.c,v 1.18 2003/09/19 06:19:56 itojun Exp $ */ /* * Copyright (c) 1992, 1993 @@ -76,9 +76,11 @@ emalloc(size_t size) void * erealloc(void *p, size_t size) { + void *q; - if ((p = realloc(p, size)) == NULL) + if ((q = realloc(p, size)) == NULL) nomem(); + p = q; return (p); } diff --git a/usr.sbin/makefs/ffs.c b/usr.sbin/makefs/ffs.c index f5bb4e15a0f5..84102ed142ee 100644 --- a/usr.sbin/makefs/ffs.c +++ b/usr.sbin/makefs/ffs.c @@ -1,4 +1,4 @@ -/* $NetBSD: ffs.c,v 1.22 2003/09/07 14:24:08 fvdl Exp $ */ +/* $NetBSD: ffs.c,v 1.23 2003/09/19 06:11:35 itojun Exp $ */ /* * Copyright (c) 2001 Wasabi Systems, Inc. @@ -67,7 +67,7 @@ #include #if defined(__RCSID) && !defined(__lint) -__RCSID("$NetBSD: ffs.c,v 1.22 2003/09/07 14:24:08 fvdl Exp $"); +__RCSID("$NetBSD: ffs.c,v 1.23 2003/09/19 06:11:35 itojun Exp $"); #endif /* !__lint */ #include @@ -918,6 +918,7 @@ ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) { struct direct de, *dp; uint16_t llen, reclen; + char *newbuf; assert (dbuf != NULL); assert (name != NULL); @@ -945,12 +946,13 @@ ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) de.d_namlen, de.d_name); if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) { - dbuf->size += DIRBLKSIZ; /* need another chunk */ if (debug & DEBUG_FS_MAKE_DIRBUF) printf("ffs_make_dirbuf: growing buf to %d\n", - dbuf->size); - if ((dbuf->buf = realloc(dbuf->buf, dbuf->size)) == NULL) + dbuf->size + DIRBLKSIZ); + if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL) err(1, "Allocating memory for directory buffer"); + dbuf->buf = newbuf; + dbuf->size += DIRBLKSIZ; memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ); dbuf->cur = dbuf->size - DIRBLKSIZ; } else { /* shrink end of previous */ diff --git a/usr.sbin/makefs/walk.c b/usr.sbin/makefs/walk.c index 343e477fe983..2e2cbe86d04d 100644 --- a/usr.sbin/makefs/walk.c +++ b/usr.sbin/makefs/walk.c @@ -1,4 +1,4 @@ -/* $NetBSD: walk.c,v 1.14 2003/08/07 11:25:32 agc Exp $ */ +/* $NetBSD: walk.c,v 1.15 2003/09/19 06:11:35 itojun Exp $ */ /* * Copyright (c) 2001 Wasabi Systems, Inc. @@ -73,7 +73,7 @@ #include #if defined(__RCSID) && !defined(__lint) -__RCSID("$NetBSD: walk.c,v 1.14 2003/08/07 11:25:32 agc Exp $"); +__RCSID("$NetBSD: walk.c,v 1.15 2003/09/19 06:11:35 itojun Exp $"); #endif /* !__lint */ #include @@ -497,7 +497,7 @@ link_check(fsinode *entry) uint32_t dev; uint32_t ino; fsinode *dup; - } *dups; + } *dups, *newdups; static int ndups, maxdups; int i; @@ -519,10 +519,11 @@ link_check(fsinode *entry) printf("link_check: no match for [%d, %d]\n", entry->st.st_dev, entry->st.st_ino); if (ndups == maxdups) { - maxdups += 128; - if ((dups = realloc(dups, sizeof(struct dupnode) * maxdups)) + if ((newdups = realloc(dups, sizeof(struct dupnode) * (maxdups + 128))) == NULL) err(1, "Memory allocation error"); + dups = newdups; + maxdups += 128; } dups[ndups].dev = entry->st.st_dev; dups[ndups].ino = entry->st.st_ino; diff --git a/usr.sbin/mtree/spec.c b/usr.sbin/mtree/spec.c index 1d3ef0a0f5ee..2ddf53fba65e 100644 --- a/usr.sbin/mtree/spec.c +++ b/usr.sbin/mtree/spec.c @@ -1,4 +1,4 @@ -/* $NetBSD: spec.c,v 1.50 2003/08/07 11:25:36 agc Exp $ */ +/* $NetBSD: spec.c,v 1.51 2003/09/19 06:15:55 itojun Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -70,7 +70,7 @@ #if 0 static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: spec.c,v 1.50 2003/08/07 11:25:36 agc Exp $"); +__RCSID("$NetBSD: spec.c,v 1.51 2003/09/19 06:15:55 itojun Exp $"); #endif #endif /* not lint */ @@ -104,7 +104,7 @@ spec(FILE *fp) NODE *centry, *last, *pathparent, *cur; char *p, *e, *next; NODE ginfo, *root; - char *buf, *tname; + char *buf, *tname, *ntname; size_t tnamelen, plen; root = NULL; @@ -162,9 +162,10 @@ noparent: mtree_err("no parent node"); plen = strlen(p) + 1; if (plen > tnamelen) { - tnamelen = plen; - if ((tname = realloc(tname, tnamelen)) == NULL) + if ((ntname = realloc(tname, plen)) == NULL) mtree_err("realloc: %s", strerror(errno)); + tname = ntname; + tnamelen = plen; } if (strunvis(tname, p) == -1) mtree_err("strunvis failed on `%s'", p);