-/* $Id: roff.c,v 1.168 2011/08/16 12:30:12 kristaps Exp $ */
+/* $Id: roff.c,v 1.172 2011/10/24 21:41:45 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
/* Maximum number of nested if-else conditionals. */
#define RSTACK_MAX 128
+/* Maximum number of string expansions per line, to break infinite loops. */
+#define EXPAND_LIMIT 1000
+
enum rofft {
ROFF_ad,
ROFF_am,
const char *, size_t);
static enum rofferr roff_line_ignore(ROFF_ARGS);
static enum rofferr roff_nr(ROFF_ARGS);
-static void roff_openeqn(struct roff *, const char *,
+static void roff_openeqn(struct roff *, const char *,
int, int, const char *);
static enum rofft roff_parse(struct roff *, const char *, int *);
static enum rofferr roff_parsetext(char *);
-static void roff_res(struct roff *,
+static enum rofferr roff_res(struct roff *,
char **, size_t *, int, int);
static enum rofferr roff_rm(ROFF_ARGS);
static void roff_setstr(struct roff *,
* is processed.
* This also checks the syntax of regular escapes.
*/
-static void
+static enum rofferr
roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
{
enum mandoc_esc esc;
const char *stnam; /* start of the name, after "[(*" */
const char *cp; /* end of the name, e.g. before ']' */
const char *res; /* the string to be substituted */
- int i, maxl;
+ int i, maxl, expand_count;
size_t nsz;
char *n;
+ expand_count = 0;
+
again:
cp = *bufp + pos;
while (NULL != (cp = strchr(cp, '\\'))) {
*/
if ('\0' == *cp)
- return;
+ return(ROFF_CONT);
if ('*' != *cp) {
res = cp;
mandoc_msg
(MANDOCERR_BADESCAPE, r->parse,
ln, (int)(stesc - *bufp), NULL);
- return;
+ return(ROFF_CONT);
}
cp++;
switch (*cp) {
case ('\0'):
- return;
+ return(ROFF_CONT);
case ('('):
cp++;
maxl = 2;
(MANDOCERR_BADESCAPE,
r->parse, ln,
(int)(stesc - *bufp), NULL);
- return;
+ return(ROFF_CONT);
}
if (0 == maxl && ']' == *cp)
break;
*bufp = n;
*szp = nsz;
- goto again;
+
+ if (EXPAND_LIMIT >= ++expand_count)
+ goto again;
+
+ /* Just leave the string unexpanded. */
+ mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
+ return(ROFF_IGN);
}
+ return(ROFF_CONT);
}
/*
static enum rofferr
roff_parsetext(char *p)
{
- char l, r;
size_t sz;
const char *start;
enum mandoc_esc esc;
continue;
}
- l = *(p - 1);
- r = *(p + 1);
- if ('\\' != l &&
- '\t' != r && '\t' != l &&
- ' ' != r && ' ' != l &&
- '-' != r && '-' != l &&
- ! isdigit((unsigned char)l) &&
- ! isdigit((unsigned char)r))
+ if (isalpha((unsigned char)p[-1]) &&
+ isalpha((unsigned char)p[1]))
*p = ASCII_HYPH;
p++;
}
* words to fill in.
*/
- roff_res(r, bufp, szp, ln, pos);
+ e = roff_res(r, bufp, szp, ln, pos);
+ if (ROFF_IGN == e)
+ return(e);
+ assert(ROFF_CONT == e);
ppos = pos;
ctl = mandoc_getcontrol(*bufp, &pos);