From 2190f576b08a82d0ae141f0497606e4a7d190b4a Mon Sep 17 00:00:00 2001 From: espie Date: Sat, 17 May 2014 20:05:07 +0000 Subject: [PATCH] fix alloc idioms. watch out for emalloc that's actually a calloc in disguise okay chl@ tedu@ --- usr.bin/column/column.c | 35 ++++++++++++++++++----------------- 1 file 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 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); } -- 2.20.1