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
This commit is contained in:
G. Branden Robinson
2025-11-09 06:38:20 -06:00
committed by Dan Cross
parent 37cd522b0a
commit f39a2407b6

View File

@@ -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();