On Wed, 27 Sep 2023 10:59:26 -0600, "Todd C. Miller" wrote: > I think we want support for arbitrary line lengths. There is only > one place where we need to reallocate the line buffer.
The correct check is for "lp - line == linesz - 1". The code will overwrite the newline with a NUL so we don't need to leave space for it explicitly. As written, deroff will not emit a line that does not end with a newline. That could be changed in a subsequent commit if it so desired. - todd Index: usr.bin/deroff/deroff.c =================================================================== RCS file: /cvs/src/usr.bin/deroff/deroff.c,v retrieving revision 1.17 diff -u -p -u -r1.17 deroff.c --- usr.bin/deroff/deroff.c 8 Mar 2023 04:43:10 -0000 1.17 +++ usr.bin/deroff/deroff.c 27 Sep 2023 19:54:20 -0000 @@ -135,7 +135,8 @@ int keepblock; /* keep blocks of text; n char chars[128]; /* SPECIAL, PUNCT, APOS, DIGIT, or LETTER */ -char line[LINE_MAX]; +size_t linesz; +char *line; char *lp; int c; @@ -342,6 +343,10 @@ main(int ac, char **av) files[0] = infile; filesp = &files[0]; + linesz = LINE_MAX; + if ((line = malloc(linesz)) == NULL) + err(1, NULL); + for (i = 'a'; i <= 'z'; ++i) chars[i] = LETTER; for (i = 'A'; i <= 'Z'; ++i) @@ -477,7 +482,15 @@ regline(void (*pfunc)(char *, int), int line[0] = c; lp = line; - while (lp - line < sizeof(line)) { + for (;;) { + if (lp - line == linesz - 1) { + char *newline = reallocarray(line, linesz, 2); + if (newline == NULL) + err(1, NULL); + lp = newline + (lp - line); + line = newline; + linesz *= 2; + } if (c == '\\') { *lp = ' '; backsl();