git: 507c611aeac7 - main - m4: Fix eval output width

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 17 Feb 2026 14:02:21 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=507c611aeac7ca9aed12353b1044bb21ab00afae

commit 507c611aeac7ca9aed12353b1044bb21ab00afae
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-02-17 14:01:34 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-02-17 14:01:42 +0000

    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
---
 usr.bin/m4/eval.c                 | 8 ++++----
 usr.bin/m4/misc.c                 | 2 --
 usr.bin/m4/tests/eval.m4          | 2 ++
 usr.bin/m4/tests/regress.eval.out | 2 ++
 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