summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2014-05-22 19:50:35 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2014-05-22 19:50:35 +0000
commitf89ec9cfdd65bc9715778ab899d2395f0457cab3 (patch)
tree4a3ab44008400b870d621a3e50e854e1a7ce1652
parent9b5eb14017210e2ee36f9f9b113db00060b3cb9f (diff)
If we have to realloc() "list" we need to zero out the new entries
to match the behavior of the initial calloc(). The "cols" array does not actually need to be zeroed so use reallocarray() instead of calloc(). Also fix a memory leak of tbl and initialize the value of maxcols/maxentry in the assignment instead of in the ecalloc() call. OK espie@
-rw-r--r--usr.bin/column/column.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/usr.bin/column/column.c b/usr.bin/column/column.c
index 527d333ce8a..f1dbed0db5d 100644
--- a/usr.bin/column/column.c
+++ b/usr.bin/column/column.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: column.c,v 1.18 2014/05/17 20:05:07 espie Exp $ */
+/* $OpenBSD: column.c,v 1.19 2014/05/22 19:50:34 millert Exp $ */
/* $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $ */
/*
@@ -206,12 +206,12 @@ maketbl(void)
TBL *t;
int coloff, cnt;
char *p, **lp;
- int *lens, maxcols;
+ int *lens, maxcols = DEFCOLS;
TBL *tbl;
char **cols;
t = tbl = ecalloc(entries, sizeof(TBL));
- cols = ecalloc((maxcols = DEFCOLS), sizeof(char *));
+ cols = ereallocarray(NULL, maxcols, sizeof(char *));
lens = ecalloc(maxcols, sizeof(int));
for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
@@ -243,6 +243,7 @@ maketbl(void)
(void)printf("%s\n", t->list[coloff]);
}
}
+ free(tbl);
free(lens);
free(cols);
}
@@ -253,12 +254,12 @@ maketbl(void)
void
input(FILE *fp)
{
- static size_t maxentry;
+ static size_t maxentry = DEFNUM;
int len;
char *p, buf[MAXLINELEN];
if (!list)
- list = ecalloc((maxentry = DEFNUM), sizeof(char *));
+ list = ecalloc(maxentry, sizeof(char *));
while (fgets(buf, MAXLINELEN, fp)) {
for (p = buf; isspace((unsigned char)*p); ++p);
if (!*p)
@@ -275,6 +276,7 @@ input(FILE *fp)
if (entries == maxentry) {
maxentry += DEFNUM;
list = ereallocarray(list, maxentry, sizeof(char *));
+ memset(list + entries, 0, DEFNUM * sizeof(char *));
}
if (!(list[entries++] = strdup(buf)))
err(1, NULL);