From: schwarze Date: Tue, 7 Sep 2021 17:05:30 +0000 (+0000) Subject: Fix an infinite loop that could occur during some cases of horizontally X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=56155c97046aef489ddfc4a3ab68372f61a36025;p=openbsd Fix an infinite loop that could occur during some cases of horizontally overlapping horizontal spans. One span would calculate a desired target width and start preparations for applying it to some columns, then the other span would overwrite the target width with a different value and also start preparations for applying that one to some columns, which could sometimes confuse the code doing the final distribution to the point of not doing anything at all before entering the next iteration. Fix this by making sure the distribution is done step by step, doing one step at a time rather than allowing multiple steps to conflict. Specifically, always do the smallest useful step first. This change also simplifies the code. For example, the local "colwidth" array is no longer needed. Note that the algorithm still differs from the one implemented in GNU tbl(1), which appears to not even try to harmonize column widths but seems to simply distribute the same amount to all constituent columns, no matter whether their intrinsic width is narrow or wide. Adopting a GNU-compatible algorithm might allow further simplifiction in addition to yielding even more similar output, but i do not want to implement any major changes of the algorithm at this time. The infinite loop was reported by . --- diff --git a/usr.bin/mandoc/out.c b/usr.bin/mandoc/out.c index 9a476b4d9f4..6d73c08a255 100644 --- a/usr.bin/mandoc/out.c +++ b/usr.bin/mandoc/out.c @@ -1,4 +1,4 @@ -/* $OpenBSD: out.c,v 1.53 2021/09/07 14:50:56 schwarze Exp $ */ +/* $OpenBSD: out.c,v 1.54 2021/09/07 17:05:30 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011, 2014, 2015, 2017, 2018, 2019, 2021 @@ -121,7 +121,6 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, const struct tbl_dat *dp; struct roffcol *col; struct tbl_colgroup *first_group, **gp, *g; - size_t *colwidth; size_t ewidth, min1, min2, wanted, width, xwidth; int done, icol, maxcol, necol, nxcol, quirkcol; @@ -255,20 +254,8 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, gp = &(*gp)->next; } - colwidth = mandoc_reallocarray(NULL, maxcol + 1, sizeof(*colwidth)); while (first_group != NULL) { - /* - * Rebuild the array of the widths of all columns - * participating in spans that require expansion. - */ - - for (icol = 0; icol <= maxcol; icol++) - colwidth[icol] = SIZE_MAX; - for (g = first_group; g != NULL; g = g->next) - for (icol = g->startcol; icol <= g->endcol; icol++) - colwidth[icol] = tbl->cols[icol].width; - /* * Find the smallest and second smallest column width * among the columns which may need expamsion. @@ -276,12 +263,12 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, min1 = min2 = SIZE_MAX; for (icol = 0; icol <= maxcol; icol++) { - if (min1 > colwidth[icol]) { + width = tbl->cols[icol].width; + if (min1 > width) { min2 = min1; - min1 = colwidth[icol]; - } else if (min1 < colwidth[icol] && - min2 > colwidth[icol]) - min2 = colwidth[icol]; + min1 = width; + } else if (min1 < width && min2 > width) + min2 = width; } /* @@ -303,26 +290,22 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, width = min2; if (wanted > width) wanted = width; - for (icol = g->startcol; icol <= g->endcol; icol++) - if (colwidth[icol] == min1 || - (colwidth[icol] < min2 && - colwidth[icol] > width)) - colwidth[icol] = width; } - /* Record the effect of the widening on the group list. */ + /* Record the effect of the widening. */ gp = &first_group; while ((g = *gp) != NULL) { done = 0; for (icol = g->startcol; icol <= g->endcol; icol++) { - if (colwidth[icol] != wanted || - tbl->cols[icol].width == wanted) + if (tbl->cols[icol].width != min1) continue; if (g->wanted <= wanted - min1) { + tbl->cols[icol].width += g->wanted; done = 1; break; } + tbl->cols[icol].width = wanted; g->wanted -= wanted - min1; } if (done) { @@ -331,14 +314,7 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, } else gp = &(*gp)->next; } - - /* Record the effect of the widening on the columns. */ - - for (icol = 0; icol <= maxcol; icol++) - if (colwidth[icol] == wanted) - tbl->cols[icol].width = wanted; } - free(colwidth); /* * Align numbers with text.