summaryrefslogtreecommitdiffstats
path: root/usr.bin/rs
diff options
context:
space:
mode:
authorru <ru@FreeBSD.org>2001-09-10 15:09:12 +0000
committerru <ru@FreeBSD.org>2001-09-10 15:09:12 +0000
commit80467e19faacc969e40894dfa6a8dc28d5e3bb97 (patch)
treeace57541b63e60922244dda30ea3165628d7f8df /usr.bin/rs
parent5fb7d8a11ae9014181ba0af5100324496c0c0786 (diff)
downloadFreeBSD-src-80467e19faacc969e40894dfa6a8dc28d5e3bb97.zip
FreeBSD-src-80467e19faacc969e40894dfa6a8dc28d5e3bb97.tar.gz
Do not overrun entry array when printing output tables.
Cleanup storage allocation for entries. Obtained from: NetBSD
Diffstat (limited to 'usr.bin/rs')
-rw-r--r--usr.bin/rs/rs.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/usr.bin/rs/rs.c b/usr.bin/rs/rs.c
index ce5a240..a1c2f30 100644
--- a/usr.bin/rs/rs.c
+++ b/usr.bin/rs/rs.c
@@ -105,6 +105,11 @@ void prints __P((char *, int));
void putfile __P((void));
static void usage __P((void));
+#define INCR(ep) do { \
+ if (++ep >= endelem) \
+ ep = getptrs(ep); \
+} while(0)
+
int
main(argc, argv)
int argc;
@@ -126,7 +131,7 @@ getfile()
{
register char *p;
register char *endp;
- register char **ep = 0;
+ char **ep;
int multisep = (flags & ONEISEPONLY ? 0 : 1);
int nullpad = flags & NULLPAD;
char **padto;
@@ -153,7 +158,8 @@ getfile()
p = curline;
do {
if (flags & ONEPERLINE) {
- *ep++ = curline;
+ *ep = curline;
+ INCR(ep); /* prepare for next entry */
if (maxlen < curlen)
maxlen = curlen;
irows++;
@@ -171,16 +177,16 @@ getfile()
*p = '\0'; /* mark end of entry */
if (maxlen < p - *ep) /* update maxlen */
maxlen = p - *ep;
- ep++; /* prepare for next entry */
+ INCR(ep); /* prepare for next entry */
}
irows++; /* update row count */
if (nullpad) { /* pad missing entries */
padto = elem + irows * icols;
- while (ep < padto)
- *ep++ = "";
+ while (ep < padto) {
+ *ep = "";
+ INCR(ep);
+ }
}
- if (ep > endelem) /* if low on pointers */
- ep = getptrs(ep); /* get some more */
} while (getline() != EOF);
*ep = 0; /* mark end of pointers */
nelem = ep - elem;
@@ -368,24 +374,16 @@ char **
getptrs(sp)
char **sp;
{
- register char **p, **ep;
+ char **p;
- for (;;) {
- allocsize += allocsize;
- if (!(p = (char **) malloc(allocsize * sizeof(char *))))
- errx(1, "malloc");
- if ((endelem = p + allocsize - icols) <= p) {
- free(p);
- continue;
- }
- if (elem != 0)
- free(elem);
- ep = elem;
- elem = p;
- while (ep < sp)
- *p++ = *ep++;
- return(p);
- }
+ allocsize += allocsize;
+ p = (char **)realloc(elem, allocsize * sizeof(char *));
+ if (p == NULL)
+ err(1, "no memory");
+
+ sp += (p - elem);
+ endelem = (elem = p) + allocsize;
+ return(sp);
}
void
OpenPOWER on IntegriCloud