diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2014-05-17 20:05:08 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2014-05-17 20:05:08 +0000 |
commit | 6f5366d7b36a861496e7a13c1f2b0a80a6add9e1 (patch) | |
tree | c93c8181c89e6d314215e2e38a4d994be5044188 /usr.bin/column/column.c | |
parent | 292dfb41d9b64a6c627a32278ae0153b603f8ca3 (diff) |
fix alloc idioms. watch out for emalloc that's actually a calloc in disguise
okay chl@ tedu@
Diffstat (limited to 'usr.bin/column/column.c')
-rw-r--r-- | usr.bin/column/column.c | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/usr.bin/column/column.c b/usr.bin/column/column.c index 87fa6f7f360..527d333ce8a 100644 --- a/usr.bin/column/column.c +++ b/usr.bin/column/column.c @@ -1,4 +1,4 @@ -/* $OpenBSD: column.c,v 1.17 2014/01/22 09:39:38 jsg Exp $ */ +/* $OpenBSD: column.c,v 1.18 2014/05/17 20:05:07 espie Exp $ */ /* $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $ */ /* @@ -42,8 +42,8 @@ #include <unistd.h> void c_columnate(void); -void *emalloc(size_t); -void *erealloc(void *, size_t); +void *ereallocarray(void *, size_t, size_t); +void *ecalloc(size_t, size_t); void input(FILE *); void maketbl(void); void print(void); @@ -210,22 +210,24 @@ maketbl(void) TBL *tbl; char **cols; - t = tbl = emalloc(entries * sizeof(TBL)); - cols = emalloc((maxcols = DEFCOLS) * sizeof(char *)); - lens = emalloc(maxcols * sizeof(int)); + t = tbl = ecalloc(entries, sizeof(TBL)); + cols = ecalloc((maxcols = DEFCOLS), 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)); p = NULL) if (++coloff == maxcols) { maxcols += DEFCOLS; - cols = erealloc(cols, maxcols * sizeof(char *)); - lens = erealloc(lens, maxcols * sizeof(int)); + cols = ereallocarray(cols, maxcols, + sizeof(char *)); + lens = ereallocarray(lens, maxcols, + sizeof(int)); memset(lens + coloff, 0, DEFCOLS * sizeof(int)); } if (coloff == 0) continue; - t->list = emalloc(coloff * sizeof(char *)); - t->len = emalloc(coloff * sizeof(int)); + t->list = ecalloc(coloff, sizeof(char *)); + t->len = ecalloc(coloff, sizeof(int)); for (t->cols = coloff; --coloff >= 0;) { t->list[coloff] = cols[coloff]; t->len[coloff] = strlen(cols[coloff]); @@ -256,7 +258,7 @@ input(FILE *fp) char *p, buf[MAXLINELEN]; if (!list) - list = emalloc((maxentry = DEFNUM) * sizeof(char *)); + list = ecalloc((maxentry = DEFNUM), sizeof(char *)); while (fgets(buf, MAXLINELEN, fp)) { for (p = buf; isspace((unsigned char)*p); ++p); if (!*p) @@ -272,7 +274,7 @@ input(FILE *fp) maxlength = len; if (entries == maxentry) { maxentry += DEFNUM; - list = erealloc(list, maxentry * sizeof(char *)); + list = ereallocarray(list, maxentry, sizeof(char *)); } if (!(list[entries++] = strdup(buf))) err(1, NULL); @@ -280,22 +282,21 @@ input(FILE *fp) } void * -emalloc(size_t size) +ereallocarray(void *oldp, size_t sz1, size_t sz2) { void *p; - if (!(p = malloc(size))) + if (!(p = reallocarray(oldp, sz1, sz2))) err(1, NULL); - memset(p, 0, size); return (p); } void * -erealloc(void *oldp, size_t size) +ecalloc(size_t sz1, size_t sz2) { void *p; - if (!(p = realloc(oldp, size))) + if (!(p = calloc(sz1, sz2))) err(1, NULL); return (p); } |