aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorG. Branden Robinson <g.branden.robinson@gmail.com>2025-11-09 06:38:20 -0600
committerDan Cross <crossd@gmail.com>2025-11-09 14:25:48 -0500
commitf39a2407b6e6ace6af68e466bfa2f362b9a9dd36 (patch)
tree0c8cf1a8b91d045e66cd20f92bb8059674a16f56
parent37cd522b0a79b4f6b2b5d4b8d27667dea44f71d4 (diff)
troff: fix SIGFPE when using modulus operator
I uncovered this problem while writing unit tests for GNU troff's delimited expression handling. Plan 9 troff's numeric expression evaluator handles division by zero but not modulus by zero. Fixes: $ echo '.if %0%0% .tm true' | 9 troff Floating point exception (core dumped) $ echo '.if 1%0 .tm true' | 9 troff Floating point exception (core dumped) After this patch: $ echo '.if %0%0% .tm true' | 9 troff x T utf x res 720 1 1 x init troff: modulus by zero.; stdin:1 troff: modulus by zero.; stdin:1 x trailer V0 x stop $ echo '.if 1%0 .tm true' | 9 troff x T utf x res 720 1 1 x init troff: modulus by zero.; stdin:1 x trailer V0 x stop
-rw-r--r--src/cmd/troff/n4.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/cmd/troff/n4.c b/src/cmd/troff/n4.c
index 08e06ad3..f555d89f 100644
--- a/src/cmd/troff/n4.c
+++ b/src/cmd/troff/n4.c
@@ -453,7 +453,12 @@ a0:
i = ckph();
if (nonumb)
break;
- acc %= i;
+ if (i == 0) {
+ flusho();
+ ERROR "modulus by zero." WARN;
+ acc = 0;
+ } else
+ acc %= i;
goto a0;
case '&': /*and*/
i = ckph();