diff options
author | Ingo Schwarze <schwarze@openbsd.org> | 2014-11-21 01:52:53 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@openbsd.org> | 2014-11-21 01:52:53 +0000 |
commit | e5162e81380b9a4f3a519fe614576af26589f06f (patch) | |
tree | 3553277f5030eec3e2d45c6e5fb10361da80d8da /term.c | |
parent | bbc2a36ee2ac85ca438a009fda76b29d2bf869d8 (diff) | |
download | mandoc-e5162e81380b9a4f3a519fe614576af26589f06f.tar.gz |
We repeatedly observed assertion crashes in the low-level terminal
output handler because the high level terminal formatters could be
tricked into setting the left margin further to the right than the
right margin. Today, jsg@ found more of these with afl.
Change the internal interface between both levels, aiming for
simplicity and robustness of the code. Treat both margins as
*independent* settings: Now, termp.offset is the requested left
margin, and termp.rmargin is the available space. Let the lower
level cope with that case of insufficient space.
Obviously, high level code that does centering or flush right
still has to do careful checks, so i did a full audit of margin
settings in the terminal formatters.
Fixes crashes caused by excessively long title or date strings in
the man(7) footer, operating system or date strings in the mdoc(7)
footer, volume strings in the man(7) or mdoc(7) header, and a few
cases related to some non-prologue macros.
Diffstat (limited to 'term.c')
-rw-r--r-- | term.c | 20 |
1 files changed, 12 insertions, 8 deletions
@@ -100,7 +100,7 @@ term_flushln(struct termp *p) size_t j; /* temporary loop index for p->buf */ size_t jhy; /* last hyph before overflow w/r/t j */ size_t maxvis; /* output position of visible boundary */ - size_t mmax; /* used in calculating bp */ + size_t rmargin; /* the rightmost of the two margins */ /* * First, establish the maximum columns of "visible" content. @@ -113,13 +113,17 @@ term_flushln(struct termp *p) * is negative, it gets sign extended. Subtracting that * very large size_t effectively adds a small number to dv. */ - assert (p->rmargin >= p->offset); - dv = p->rmargin - p->offset; + rmargin = p->rmargin > p->offset ? p->rmargin : p->offset; + dv = p->rmargin - p->offset; maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0; - dv = p->maxrmargin - p->offset; - mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0; - bp = TERMP_NOBREAK & p->flags ? mmax : maxvis; + if (p->flags & TERMP_NOBREAK) { + dv = p->maxrmargin > p->offset ? + p->maxrmargin - p->offset : 0; + bp = (int)dv > p->overstep ? + dv - (size_t)p->overstep : 0; + } else + bp = maxvis; /* * Calculate the required amount of padding. @@ -188,8 +192,8 @@ term_flushln(struct termp *p) (*p->endline)(p); p->viscol = 0; if (TERMP_BRIND & p->flags) { - vbl = p->rmargin; - vend += p->rmargin - p->offset; + vbl = rmargin; + vend += rmargin - p->offset; } else vbl = p->offset; |