From: espie Date: Sun, 18 May 2014 09:29:54 +0000 (+0000) Subject: use reallocarray and friends. okay miod@ X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c811dcf305a019c0baa5e218aee09be4a6a091c3;p=openbsd use reallocarray and friends. okay miod@ --- diff --git a/usr.sbin/config/config.h b/usr.sbin/config/config.h index 49d2b373141..f078f586afd 100644 --- a/usr.sbin/config/config.h +++ b/usr.sbin/config/config.h @@ -1,4 +1,4 @@ -/* $OpenBSD: config.h,v 1.26 2013/10/29 15:37:56 espie Exp $ */ +/* $OpenBSD: config.h,v 1.27 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: config.h,v 1.30 1997/02/02 21:12:30 thorpej Exp $ */ /* @@ -369,7 +369,8 @@ void initsem(void); /* util.c */ void *emalloc(size_t); -void *erealloc(void *, size_t); +void *ereallocarray(void *, size_t, size_t); +void *ecalloc(size_t, size_t); char *sourcepath(const char *); void error(const char *, ...); /* immediate errs */ void xerror(const char *, int, const char *, ...); /* delayed errs */ diff --git a/usr.sbin/config/hash.c b/usr.sbin/config/hash.c index 5111175af21..e9fda768a6b 100644 --- a/usr.sbin/config/hash.c +++ b/usr.sbin/config/hash.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hash.c,v 1.15 2011/10/02 22:20:49 edd Exp $ */ +/* $OpenBSD: hash.c,v 1.16 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: hash.c,v 1.4 1996/11/07 22:59:43 gwr Exp $ */ /* @@ -128,7 +128,7 @@ ht_init(struct hashtab *ht, size_t sz) struct hashent **h; u_int n; - h = emalloc(sz * sizeof *h); + h = ereallocarray(NULL, sz, sizeof *h); ht->ht_tab = h; ht->ht_size = sz; ht->ht_mask = sz - 1; @@ -148,9 +148,7 @@ ht_expand(struct hashtab *ht) u_int n, i; n = ht->ht_size * 2; - h = emalloc(n * sizeof *h); - for (i = 0; i < n; i++) - h[i] = NULL; + h = ecalloc(n, sizeof *h); oldh = ht->ht_tab; n--; for (i = ht->ht_size; i != 0; i--) { diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c index d07cb8abcc7..0644a230cd4 100644 --- a/usr.sbin/config/main.c +++ b/usr.sbin/config/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.46 2013/11/23 17:38:15 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.47 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: main.c,v 1.22 1997/02/02 21:12:33 thorpej Exp $ */ /* @@ -735,7 +735,7 @@ optiondelta(void) for (nnewopts = 0, nv = options; nv != NULL; nv = nv->nv_next) nnewopts++; - newopts = (struct opt *)emalloc(nnewopts * sizeof(struct opt)); + newopts = ereallocarray(NULL, nnewopts, sizeof(struct opt)); if (newopts == NULL) ret = 0; for (i = 0, nv = options; nv != NULL; nv = nv->nv_next, i++) { diff --git a/usr.sbin/config/mkioconf.c b/usr.sbin/config/mkioconf.c index a5b8a7964e6..ddc9ec586ea 100644 --- a/usr.sbin/config/mkioconf.c +++ b/usr.sbin/config/mkioconf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mkioconf.c,v 1.31 2011/10/02 22:20:50 edd Exp $ */ +/* $OpenBSD: mkioconf.c,v 1.32 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: mkioconf.c,v 1.41 1996/11/11 14:18:49 mycroft Exp $ */ /* @@ -201,7 +201,7 @@ addlocname(const char *name) if (locnames == NULL || nlocnames+1 > maxlocnames) { maxlocnames *= 4; - locnames = (char **)erealloc(locnames, maxlocnames * sizeof(char *)); + locnames = ereallocarray(locnames, maxlocnames, sizeof(char *)); } for (i = 0; i < nlocnames; i++) if (strcmp(name, locnames[i]) == 0) @@ -219,7 +219,7 @@ addlocnami(short index) { if (locnami == NULL || nlocnami+1 > maxlocnami) { maxlocnami *= 4; - locnami = (short *)erealloc(locnami, maxlocnami * sizeof(short)); + locnami = ereallocarray(locnami, maxlocnami, sizeof(short)); } locnami[nlocnami++] = index; } diff --git a/usr.sbin/config/pack.c b/usr.sbin/config/pack.c index cd1937d3cae..93a74e6e64d 100644 --- a/usr.sbin/config/pack.c +++ b/usr.sbin/config/pack.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pack.c,v 1.16 2011/10/02 22:20:50 edd Exp $ */ +/* $OpenBSD: pack.c,v 1.17 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: pack.c,v 1.5 1996/08/31 21:15:11 mycroft Exp $ */ /* @@ -144,12 +144,12 @@ pack(void) } /* Allocate and pack loc[]. */ - locators.vec = emalloc(locspace * sizeof(*locators.vec)); + locators.vec = ereallocarray(NULL, locspace, sizeof(*locators.vec)); locators.used = 0; packlocs(); /* Allocate and pack pv[]. */ - parents.vec = emalloc(pvecspace * sizeof(*parents.vec)); + parents.vec = ereallocarray(NULL, pvecspace, sizeof(*parents.vec)); parents.used = 0; packpvec(); } @@ -167,7 +167,7 @@ packdevi(void) struct deva *d; int j, m, n; - packed = emalloc((ndevi + 1) * sizeof *packed); + packed = ereallocarray(NULL, ndevi + 1, sizeof *packed); n = 0; for (d = alldevas; d != NULL; d = d->d_next) { /* @@ -247,7 +247,7 @@ addparents(struct devi *src, struct devi *dst) return; if (src->i_atdev != NULL) { n = nparents(NULL, src->i_atdev, src->i_atunit); - p = emalloc(n * sizeof *p); + p = ereallocarray(NULL, n, sizeof *p); if (n == 0) return; (void)nparents(p, src->i_atdev, src->i_atunit); @@ -257,7 +257,7 @@ addparents(struct devi *src, struct devi *dst) n += nparents(NULL, nv->nv_ptr, src->i_atunit); if (n == 0) return; - p = emalloc(n * sizeof *p); + p = ereallocarray(NULL, n, sizeof *p); n = 0; for (nv = src->i_atattr->a_refs; nv != NULL; nv = nv->nv_next) n += nparents(p + n, nv->nv_ptr, src->i_atunit); @@ -283,7 +283,7 @@ addparents(struct devi *src, struct devi *dst) free(p); return; } - dst->i_parents = q = erealloc(dst->i_parents, (new + 1) * sizeof(*q)); + dst->i_parents = q = ereallocarray(dst->i_parents, new + 1, sizeof(*q)); dst->i_pvlen = new; q[new] = NULL; q += old; @@ -343,7 +343,7 @@ packpvec(void) int l, v, o; short *vec; - vec = emalloc(longest_pvec * sizeof(*vec)); + vec = ereallocarray(NULL, longest_pvec, sizeof(*vec)); qsort(packed, npacked, sizeof *packed, pvlencmp); for (p = packed; (i = *p) != NULL; p++) { l = i->i_pvlen; diff --git a/usr.sbin/config/sem.c b/usr.sbin/config/sem.c index a78198812fc..328d5f73f1a 100644 --- a/usr.sbin/config/sem.c +++ b/usr.sbin/config/sem.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sem.c,v 1.33 2013/11/23 17:38:15 deraadt Exp $ */ +/* $OpenBSD: sem.c,v 1.34 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: sem.c,v 1.10 1996/11/11 23:40:11 gwr Exp $ */ /* @@ -1099,7 +1099,8 @@ fixloc(const char *name, struct attr *attr, struct nvlist *got) if (attr->a_loclen == 0) /* e.g., "at root" */ lp = nullvec; else - lp = emalloc((attr->a_loclen + 1) * sizeof(const char *)); + lp = ereallocarray(NULL, attr->a_loclen + 1, + sizeof(const char *)); for (n = got; n != NULL; n = n->nv_next) n->nv_int = -1; nmissing = 0; diff --git a/usr.sbin/config/util.c b/usr.sbin/config/util.c index 56633058944..9bd15a81b12 100644 --- a/usr.sbin/config/util.c +++ b/usr.sbin/config/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.13 2011/10/02 22:20:50 edd Exp $ */ +/* $OpenBSD: util.c,v 1.14 2014/05/18 09:29:54 espie Exp $ */ /* $NetBSD: util.c,v 1.5 1996/08/31 20:58:29 mycroft Exp $ */ /* @@ -69,13 +69,26 @@ emalloc(size_t size) } /* - * Realloc, with abort on error. + * Reallocarray, with abort on error. */ void * -erealloc(void *p, size_t size) +ereallocarray(void *p, size_t sz1, size_t sz2) { - if ((p = realloc(p, size)) == NULL) + if ((p = reallocarray(p, sz1, sz2)) == NULL) + nomem(); + return (p); +} + +/* + * Calloc, with abort on error. + */ +void * +ecalloc(size_t sz1, size_t sz2) +{ + void *p; + + if ((p = calloc(sz1, sz2)) == NULL) nomem(); return (p); }