Prevent unsigned integer underflow when a number is too wide
authorschwarze <schwarze@openbsd.org>
Wed, 24 Dec 2014 15:37:23 +0000 (15:37 +0000)
committerschwarze <schwarze@openbsd.org>
Wed, 24 Dec 2014 15:37:23 +0000 (15:37 +0000)
for a table cell with an "nz" layout specification,
causing essentially infinite output as found by jsg@ with afl.

regress/usr.bin/mandoc/tbl/Makefile
regress/usr.bin/mandoc/tbl/misalign.in [new file with mode: 0644]
regress/usr.bin/mandoc/tbl/misalign.out_ascii [new file with mode: 0644]
usr.bin/mandoc/tbl_term.c

index 95f1bb4..4213a06 100644 (file)
@@ -1,8 +1,18 @@
-# $OpenBSD: Makefile,v 1.5 2014/12/16 23:44:16 schwarze Exp $
+# $OpenBSD: Makefile,v 1.6 2014/12/24 15:37:23 schwarze Exp $
 
-REGRESS_TARGETS         = center fonts macro nested numbers span vert
+REGRESS_TARGETS         = center fonts macro misalign nested numbers span vert
 LINT_TARGETS    = macro nested
-SKIP_GROFF      = nested
+
+# groff-1.22.3 defect:
+# - When space is insufficient (on either side) for properly aligning
+#   a number, GNU tbl(1) moves the number too much to the right,
+#   overflowing the column, even if space would be sufficient without
+#   left padding.
+
+# trivial difference to groff-1.22.3:
+# .TS in a table causes a blank table line in GNU tbl(1), but not in mandoc.
+
+SKIP_GROFF      = misalign nested
 SKIP_TMAN      ?= ALL
 
 TBL = /usr/local/bin/tbl
diff --git a/regress/usr.bin/mandoc/tbl/misalign.in b/regress/usr.bin/mandoc/tbl/misalign.in
new file mode 100644 (file)
index 0000000..d7d3226
--- /dev/null
@@ -0,0 +1,16 @@
+.TH TBL-MISALIGN 1 "December 24, 2014" OpenBSD
+.SH NAME
+tbl-misalign \- failing alignment in tables
+.SH DESCRIPTION
+normal text
+.TS
+box tab(:);
+n, nz.
+12.34
+_
+100.0
+0.001
+_
+1000.0
+0.0001
+.TE
diff --git a/regress/usr.bin/mandoc/tbl/misalign.out_ascii b/regress/usr.bin/mandoc/tbl/misalign.out_ascii
new file mode 100644 (file)
index 0000000..324ee7e
--- /dev/null
@@ -0,0 +1,22 @@
+TBL-MISALIGN(1)             General Commands Manual            TBL-MISALIGN(1)
+
+
+
+N\bNA\bAM\bME\bE
+       tbl-misalign - failing alignment in tables
+
+D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
+       normal text
+
+       +------+
+       |12.34 |
+       +------+
+       |100.0 |
+       |0.001 |
+       +------+
+       |1000.0 |
+       |0.0001 |
+       +------+
+
+
+OpenBSD                        December 24, 2014               TBL-MISALIGN(1)
index 0bc8a1c..c6c6d2b 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: tbl_term.c,v 1.19 2014/10/14 18:16:57 schwarze Exp $ */
+/*     $OpenBSD: tbl_term.c,v 1.20 2014/12/24 15:37:23 schwarze Exp $ */
 /*
  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -415,9 +415,13 @@ tbl_number(struct termp *tp, const struct tbl_opts *opts,
        } else
                d = sz + psz;
 
-       padl = col->decimal - d;
-
-       tbl_char(tp, ASCII_NBRSP, padl);
+       if (col->decimal > d && col->width > sz) {
+               padl = col->decimal - d;
+               if (padl + sz > col->width)
+                       padl = col->width - sz;
+               tbl_char(tp, ASCII_NBRSP, padl);
+       } else
+               padl = 0;
        tbl_word(tp, dp);
        if (col->width > sz + padl)
                tbl_char(tp, ASCII_NBRSP, col->width - sz - padl);