aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-02-17 14:01:34 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-02-17 14:01:42 +0000
commit507c611aeac7ca9aed12353b1044bb21ab00afae (patch)
tree42e16c318287f8763738cccc35a6c88c6c3d09b9
parent62fba0054d9eb2303116f54be1f9bc0e7b75cc15 (diff)
m4: Fix eval output width
According to POSIX, the optional third argument is the minimum number of digits to print regardless of sign. We interpreted it as the minimum width of the output including the sign. Additionally, the variable used to hold this value was confusingly named “maxdigits”. PR: 293214 MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D55311
-rw-r--r--usr.bin/m4/eval.c8
-rw-r--r--usr.bin/m4/misc.c2
-rw-r--r--usr.bin/m4/tests/eval.m42
-rw-r--r--usr.bin/m4/tests/regress.eval.out2
4 files changed, 8 insertions, 6 deletions
diff --git a/usr.bin/m4/eval.c b/usr.bin/m4/eval.c
index 4e45a71874e1..0963a61a2914 100644
--- a/usr.bin/m4/eval.c
+++ b/usr.bin/m4/eval.c
@@ -178,7 +178,7 @@ expand_builtin(const char *argv[], int argc, int td)
*/
{
int base = 10;
- int maxdigits = 0;
+ int mindigits = 0;
const char *errstr;
if (argc > 3 && *argv[3] != '\0') {
@@ -189,14 +189,14 @@ expand_builtin(const char *argv[], int argc, int td)
}
}
if (argc > 4) {
- maxdigits = strtonum(argv[4], 0, INT_MAX, &errstr);
+ mindigits = strtonum(argv[4], 0, INT_MAX, &errstr);
if (errstr) {
- m4errx(1, "expr: maxdigits is %s: %s.",
+ m4errx(1, "expr: mindigits is %s: %s.",
errstr, argv[4]);
}
}
if (argc > 2)
- pbnumbase(expr(argv[2]), base, maxdigits);
+ pbnumbase(expr(argv[2]), base, mindigits);
break;
}
diff --git a/usr.bin/m4/misc.c b/usr.bin/m4/misc.c
index 3091f2ad1f9e..fd72292aeac0 100644
--- a/usr.bin/m4/misc.c
+++ b/usr.bin/m4/misc.c
@@ -138,8 +138,6 @@ pbnumbase(int n, int base, int d)
}
while ((num /= base) > 0);
- if (n < 0)
- printed++;
while (printed++ < d)
pushback('0');
diff --git a/usr.bin/m4/tests/eval.m4 b/usr.bin/m4/tests/eval.m4
index 1d3f886d0d89..dc0fada781f1 100644
--- a/usr.bin/m4/tests/eval.m4
+++ b/usr.bin/m4/tests/eval.m4
@@ -3,3 +3,5 @@ dnl expr parser
eval(224&127)
eval(224|127)
eval(224&&127)
+eval(3-2, 10, 5)
+eval(2-3, 10, 4)
diff --git a/usr.bin/m4/tests/regress.eval.out b/usr.bin/m4/tests/regress.eval.out
index 7298b3f43840..b1bb211dcb64 100644
--- a/usr.bin/m4/tests/regress.eval.out
+++ b/usr.bin/m4/tests/regress.eval.out
@@ -1,3 +1,5 @@
96
255
1
+00001
+-0001