=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.68 retrieving revision 1.70 diff -u -p -r1.68 -r1.70 --- texi2mdoc/main.c 2015/03/12 10:44:34 1.68 +++ texi2mdoc/main.c 2015/03/13 08:13:21 1.70 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.68 2015/03/12 10:44:34 kristaps Exp $ */ +/* $Id: main.c,v 1.70 2015/03/13 08:13:21 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -607,9 +607,17 @@ domacro(struct texi *p, enum texicmd cmd, size_t *pos) m.key[end - start] = '\0'; m.args = argparse(p, pos, &m.argsz, 0); + if (*pos == BUFSZ(p)) { + texiwarn(p, "unexpected EOF"); + return; + } /* Note: we advance to the beginning of the macro. */ advanceeoln(p, pos, 1); + if ((start = *pos) == BUFSZ(p)) { + texiwarn(p, "unexpected EOF"); + return; + } /* * According to the Texinfo manual, the macro ends on the @@ -622,7 +630,6 @@ domacro(struct texi *p, enum texicmd cmd, size_t *pos) * @end macro without the leading newline else we might look * past empty macros. */ - start = *pos; endtok = "@end macro\n"; endtoksz = strlen(endtok); blk = memmem(&BUF(p)[start], BUFSZ(p) - start, endtok, endtoksz); @@ -1509,12 +1516,16 @@ indexcmp(const void *p1, const void *p2) static void doprintindex(struct texi *p, enum texicmd cmd, size_t *pos) { - size_t i, j, start, end, len; + static size_t guard = 0; + size_t i, j, start, end, len; #if HAVE_INDEX - char *cp; - char buf[PATH_MAX]; + char *cp; + char buf[PATH_MAX]; #endif + if (guard++ > 8) + texierr(p, "recursive @printindex"); + while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) advance(p, pos); start = *pos; @@ -1522,12 +1533,18 @@ doprintindex(struct texi *p, enum texicmd cmd, size_t advance(p, pos); if ((end = *pos) == BUFSZ(p)) { texiwarn(p, "unexpected EOF"); + guard--; return; } advance(p, pos); - if (0 == (len = end - start)) { + if (*pos == BUFSZ(p)) { + texiwarn(p, "unexpected EOF"); + guard--; + return; + } else if (0 == (len = end - start)) { texiwarn(p, "zero-length index"); + guard--; return; } @@ -1542,9 +1559,12 @@ doprintindex(struct texi *p, enum texicmd cmd, size_t if (i == p->indexsz) { texiwarn(p, "cannot find index"); + guard--; return; - } else if (0 == p->indexs[i].indexsz) + } else if (0 == p->indexs[i].indexsz) { + guard--; return; + } /* Alphabetically sort our indices. */ qsort(p->indexs[i].index, @@ -1591,6 +1611,7 @@ doprintindex(struct texi *p, enum texicmd cmd, size_t p->seenvs = 0; teximacro(p, "El"); texivspace(p); + guard--; } static void @@ -1672,7 +1693,7 @@ domenu(struct texi *p, enum texicmd cmd, size_t *pos) while (*pos < BUFSZ(p)) { while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) advance(p, pos); - if ('*' != BUF(p)[*pos]) { + if (*pos < BUFSZ(p) && '*' != BUF(p)[*pos]) { if (TEXICMD_END == peeklinecmd(p, *pos)) break; parseeoln(p, pos); @@ -1695,7 +1716,10 @@ domenu(struct texi *p, enum texicmd cmd, size_t *pos) p->seenws = *pos < BUFSZ(p) && isws(BUF(p)[*pos]); while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) advance(p, pos); - if ('*' != BUF(p)[*pos]) { + if (*pos == BUFSZ(p)) { + texiwarn(p, "unexpected EOF"); + return; + } else if ('*' != BUF(p)[*pos]) { tcmd = peeklinecmd(p, *pos); if (TEXICMD_END == tcmd) break; @@ -1714,6 +1738,10 @@ domenu(struct texi *p, enum texicmd cmd, size_t *pos) while (*pos < BUFSZ(p) && ':' != BUF(p)[*pos]) advance(p, pos); entrynameend = *pos; + if (*pos == BUFSZ(p)) { + texiwarn(p, "unexpected EOF"); + return; + } advance(p, pos); p->seenvs = 0;