summaryrefslogtreecommitdiffstats
path: root/usr.bin/rs
diff options
context:
space:
mode:
authorjh <jh@FreeBSD.org>2011-02-07 18:10:18 +0000
committerjh <jh@FreeBSD.org>2011-02-07 18:10:18 +0000
commitd8708af48420e2edefa6527dd71ac07b1c5353eb (patch)
treea18d4881fa1efcaf161e542ce94b84e29f6815c4 /usr.bin/rs
parentc19aa41514f081f5b93a92ac8186335bcf420acf (diff)
downloadFreeBSD-src-d8708af48420e2edefa6527dd71ac07b1c5353eb.zip
FreeBSD-src-d8708af48420e2edefa6527dd71ac07b1c5353eb.tar.gz
- Use LINE_MAX from limits.h as the maximum line length instead of
BUFSIZ. Use LINE_MAX * 2 as the buffer size (BSIZE). - Error out if we encounter a line longer than LINE_MAX. The previous behavior was to silently split long lines and produce corrupted output. PR: bin/151384
Diffstat (limited to 'usr.bin/rs')
-rw-r--r--usr.bin/rs/rs.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/usr.bin/rs/rs.c b/usr.bin/rs/rs.c
index 23fa270..46de07e 100644
--- a/usr.bin/rs/rs.c
+++ b/usr.bin/rs/rs.c
@@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <ctype.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -328,8 +329,8 @@ prepfile(void)
warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
}
-#define BSIZE 2048
-char ibuf[BSIZE]; /* two screenfuls should do */
+#define BSIZE (LINE_MAX * 2)
+char ibuf[BSIZE];
int
getline(void) /* get line; maintain curline, curlen; manage storage */
@@ -350,7 +351,7 @@ getline(void) /* get line; maintain curline, curlen; manage storage */
curline = ibuf;
}
}
- if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
+ if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
/*ww = endblock-curline; tt += ww;*/
/*printf("#wasted %d total %d\n",ww,tt);*/
if (!(curline = (char *) malloc(BSIZE)))
@@ -358,11 +359,16 @@ getline(void) /* get line; maintain curline, curlen; manage storage */
endblock = curline + BSIZE;
/*printf("#endb %d curline %d\n",endblock,curline);*/
}
- for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
- if ((c = getchar()) == EOF || c == '\n')
+ for (p = curline, i = 0;; *p++ = c, i++) {
+ if ((c = getchar()) == EOF)
break;
+ if (i >= LINE_MAX)
+ errx(1, "maximum line length (%d) exceeded", LINE_MAX);
+ if (c == '\n')
+ break;
+ }
*p = '\0';
- curlen = i - 1;
+ curlen = i;
return(c);
}
OpenPOWER on IntegriCloud