From f39a2407b6e6ace6af68e466bfa2f362b9a9dd36 Mon Sep 17 00:00:00 2001 From: "G. Branden Robinson" Date: Sun, 9 Nov 2025 06:38:20 -0600 Subject: 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 --- src/cmd/troff/n4.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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(); -- cgit v1.2.3