From: deraadt Date: Fri, 19 Dec 2014 19:28:55 +0000 (+0000) Subject: Use reallocarray() where suitable X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=e6a8808bf8933b6e31d74620095f346692f6e6a3;p=openbsd Use reallocarray() where suitable ok millert doug --- diff --git a/usr.bin/awk/b.c b/usr.bin/awk/b.c index 29a8cec51e8..28ac2d6b93b 100644 --- a/usr.bin/awk/b.c +++ b/usr.bin/awk/b.c @@ -1,4 +1,4 @@ -/* $OpenBSD: b.c,v 1.17 2011/09/28 19:27:18 millert Exp $ */ +/* $OpenBSD: b.c,v 1.18 2014/12/19 19:28:55 deraadt Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -348,11 +348,13 @@ void cfoll(fa *f, Node *v) /* enter follow set of each leaf of vertex v into lfo f->re[info(v)].ltype = type(v); f->re[info(v)].lval.np = right(v); while (f->accept >= maxsetvec) { /* guessing here! */ - maxsetvec *= 4; - setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); - tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); + setvec = reallocarray(setvec, maxsetvec, + 4 * sizeof(int)); + tmpset = reallocarray(tmpset, maxsetvec, + 4 * sizeof(int)); if (setvec == 0 || tmpset == 0) overflo("out of space in cfoll()"); + maxsetvec *= 4; } for (i = 0; i <= f->accept; i++) setvec[i] = 0; @@ -389,11 +391,13 @@ int first(Node *p) /* collects initially active leaves of p into setvec */ LEAF lp = info(p); /* look for high-water mark of subscripts */ while (setcnt >= maxsetvec || lp >= maxsetvec) { /* guessing here! */ - maxsetvec *= 4; - setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); - tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); + setvec = reallocarray(setvec, maxsetvec, + 4 * sizeof(int)); + tmpset = reallocarray(tmpset, maxsetvec, + 4 * sizeof(int)); if (setvec == 0 || tmpset == 0) overflo("out of space in first()"); + maxsetvec *= 4; } if (type(p) == EMPTYRE) { setvec[lp] = 0; @@ -857,11 +861,11 @@ int cgoto(fa *f, int s, int c) assert(c == HAT || c < NCHARS); while (f->accept >= maxsetvec) { /* guessing here! */ - maxsetvec *= 4; - setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); - tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); + setvec = reallocarray(setvec, maxsetvec, 4 * sizeof(int)); + tmpset = reallocarray(tmpset, maxsetvec, 4 * sizeof(int)); if (setvec == 0 || tmpset == 0) overflo("out of space in cgoto()"); + maxsetvec *= 4; } for (i = 0; i <= f->accept; i++) setvec[i] = 0; @@ -879,11 +883,13 @@ int cgoto(fa *f, int s, int c) q = f->re[p[i]].lfollow; for (j = 1; j <= *q; j++) { if (q[j] >= maxsetvec) { - maxsetvec *= 4; - setvec = (int *) realloc(setvec, maxsetvec * sizeof(int)); - tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(int)); + setvec = reallocarray(setvec, + maxsetvec, 4 * sizeof(int)); + tmpset = reallocarray(tmpset, + maxsetvec, 4 * sizeof(int)); if (setvec == 0 || tmpset == 0) overflo("cgoto overflow"); + maxsetvec *= 4; } if (setvec[q[j]] == 0) { setcnt++; diff --git a/usr.bin/awk/run.c b/usr.bin/awk/run.c index 1500644579e..83bebb8b52f 100644 --- a/usr.bin/awk/run.c +++ b/usr.bin/awk/run.c @@ -1,4 +1,4 @@ -/* $OpenBSD: run.c,v 1.37 2014/12/08 21:50:09 deraadt Exp $ */ +/* $OpenBSD: run.c,v 1.38 2014/12/19 19:28:55 deraadt Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -270,8 +270,8 @@ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */ fp++; /* now ok to up frame */ if (fp >= frame + nframe) { int dfp = fp - frame; /* old index */ - frame = (struct Frame *) - realloc((char *) frame, (nframe += 100) * sizeof(struct Frame)); + frame = reallocarray(frame, (nframe += 100), + sizeof(struct Frame)); if (frame == NULL) FATAL("out of space for stack frames in %s", s); fp = frame + dfp;