Bug 293214 - m4 eval(neg,radix,min) does not pad wide enough
Summary: m4 eval(neg,radix,min) does not pad wide enough
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: Normal Affects Some People
Assignee: Dag-Erling Smørgrav
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-02-16 16:22 UTC by eblake
Modified: 2026-02-26 04:36 UTC (History)
1 user (show)

See Also:
des: mfc-stable15+
des: mfc-stable14+
des: mfc-stable13+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description eblake 2026-02-16 16:22:58 UTC
The POSIX wording for m4 eval (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/m4.html) includes "The third argument, if specified, sets the minimum number of digits in the result." which mirrors the wording for specifying precision as in printf("%.*d", 4, -10) outputting "-0010" with four digits but overall width 5.  GNU m4 1.4.5 changed its implementation to comply with that POSIX wording back in 2006.  However, BSD m4 still behaves as if it is using printf("%0*d", 4, -10) which outputs "-010" (ie. a minimum field width with zero-padding, rather than minimum number of digits).

While minor, this difference in behavior means that BSD m4 cannot always be used as a drop-in replacement to GNU m4, on tests such as:

echo 'eval(-10, 10, 4) | m4'
Comment 1 commit-hook freebsd_committer freebsd_triage 2026-02-17 14:02:33 UTC
A commit in branch main references this bug:

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(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2026-02-26 04:22:57 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8124bd4524a299a647fb25f8d4ebe2cfe6846a59

commit 8124bd4524a299a647fb25f8d4ebe2cfe6846a59
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-26 04:02:28 +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

    (cherry picked from commit 507c611aeac7ca9aed12353b1044bb21ab00afae)

 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(-)
Comment 3 commit-hook freebsd_committer freebsd_triage 2026-02-26 04:22:58 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=15717dd5c628ce57d8b73b6b8a8ea7e9d8f5e52a

commit 15717dd5c628ce57d8b73b6b8a8ea7e9d8f5e52a
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-26 04:03: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

    (cherry picked from commit 507c611aeac7ca9aed12353b1044bb21ab00afae)

 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(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2026-02-26 04:24:01 UTC
A commit in branch stable/13 references this bug:

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

commit c550d07d8d5f152b4b89f6c737188c30af199e3a
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-26 04:03:51 +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

    (cherry picked from commit 507c611aeac7ca9aed12353b1044bb21ab00afae)

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