Don't let the modulo operator divide by zero.
authorschwarze <schwarze@openbsd.org>
Thu, 18 Dec 2014 17:43:07 +0000 (17:43 +0000)
committerschwarze <schwarze@openbsd.org>
Thu, 18 Dec 2014 17:43:07 +0000 (17:43 +0000)
Found by jsg@ with afl.

regress/usr.bin/mandoc/roff/nr/divzero.in
regress/usr.bin/mandoc/roff/nr/divzero.out_ascii
regress/usr.bin/mandoc/roff/nr/divzero.out_lint
usr.bin/mandoc/roff.c

index d8983f1..8f776bf 100644 (file)
@@ -1,7 +1,8 @@
-.TH NR-DIVZERO 1 "October 19, 2014" OpenBSD
+.TH NR-DIVZERO 1 "December 18, 2014" OpenBSD
 .SH NAME
 nr-divzero \- division by zero in numerical expression
 .SH DESCRIPTION
 initial text
-.nr result 1/0
-final \n[result] text
+.nr divresult 1/0
+.nr modresult 1%0
+final \n[divresult] \n[modresult] text
index fe0cbbb..26d93c2 100644 (file)
@@ -6,8 +6,8 @@ N\bNA\bAM\bME\bE
        nr-divzero - division by zero in numerical expression
 
 D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
-       initial text final 0 text
+       initial text final 0 text
 
 
 
-OpenBSD                        October 19, 2014                  NR-DIVZERO(1)
+OpenBSD                        December 18, 2014                 NR-DIVZERO(1)
index aa56502..8177c0b 100644 (file)
@@ -1 +1,2 @@
 mandoc: divzero.in:6:4: ERROR: divide by zero: 1/0
+mandoc: divzero.in:7:4: ERROR: divide by zero: 1%0
index 10b566a..edd8b40 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: roff.c,v 1.115 2014/12/16 23:44:16 schwarze Exp $ */
+/*     $OpenBSD: roff.c,v 1.116 2014/12/18 17:43:07 schwarze Exp $ */
 /*
  * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -1574,7 +1574,7 @@ roff_evalnum(struct roff *r, int ln, const char *v,
                        *res *= operand2;
                        break;
                case '/':
-                       if (0 == operand2) {
+                       if (operand2 == 0) {
                                mandoc_msg(MANDOCERR_DIVZERO,
                                        r->parse, ln, *pos, v);
                                *res = 0;
@@ -1583,6 +1583,12 @@ roff_evalnum(struct roff *r, int ln, const char *v,
                        *res /= operand2;
                        break;
                case '%':
+                       if (operand2 == 0) {
+                               mandoc_msg(MANDOCERR_DIVZERO,
+                                       r->parse, ln, *pos, v);
+                               *res = 0;
+                               break;
+                       }
                        *res %= operand2;
                        break;
                case '<':