NetBSD Problem Report #60921

From www@netbsd.org  Sat Oct 24 16:17:23 2026
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits)
	 client-signature RSA-PSS (2048 bits))
	(Client CN "mail.netbsd.org", Issuer "YR1" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id C7FCC1A923C
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 24 Oct 2026 16:17:22 +0000 (UTC)
Message-Id: <20261024161721.F118E1A923D@mollari.NetBSD.org>
Date: Sat, 24 Oct 2026 16:17:21 +0000 (UTC)
From: phil@hagelb.org
Reply-To: phil@hagelb.org
To: gnats-bugs@NetBSD.org
Subject: m4 --undefine argument does not undefine
X-Send-Pr-Version: www-1.0
X-From4GNATS: "phil@hagelb.org via gnats" <gnats-admin@NetBSD.org>

>Number:         60921
>Category:       bin
>Synopsis:       m4 --undefine argument does not undefine
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Oct 24 16:20:00 +0000 2026
>Last-Modified:  Sun Oct 25 23:20:01 +0000 2026
>Originator:     Phil Hagelberg
>Release:        11.0
>Organization:
>Environment:
NetBSD coronacht 11.0 NetBSD 11.0 (GENERIC) #0: Thu Jul 30 15:23:12 UTC 2026  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
According to the man page, m4 accepts a -U or --undefine argument which will take whatever symbol it's given and remove whatever definition is attached to it. Unfortunately, it does not do that.
>How-To-Repeat:
If you run this:

echo "feeling very dnl today" | m4

As you might expect it just prints "feeling very " because dnl discards the rest of the line.

However, you should be able to run this:

echo "feeling very dnl today" | m4 --undefine dnl

and have it emit "feeling very dnl today", and indeed other implementations of m4 do this. But NetBSD's m4 does not.
>Fix:

>Audit-Trail:
From: mlelstv@serpens.de (Michael van Elst)
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/60921: m4 --undefine argument does not undefine
Date: Sun, 25 Oct 2026 08:52:23 -0000 (UTC)

 gnats-admin@NetBSD.org ("phil@hagelb.org via gnats") writes:

 >However, you should be able to run this:

 >echo "feeling very dnl today" | m4 --undefine dnl


 No idea it that should be allowed, but the behaviour changed some
 time ago.

 Maybe:

 diff -p -u -r1.52 main.c
 --- main.c      10 Jun 2026 22:25:02 -0000      1.52
 +++ main.c      25 Oct 2026 08:40:53 -0000
 @@ -228,6 +228,8 @@ main(int argc, char *argv[])
         outfile = NULL;
         resizedivs(MAXOUT);

 +       initkwds();
 +
         while ((c = getopt_long(argc, argv, "D:d:EF:GgI:iL:o:PR:Qst:U:v",
             longopts, NULL)) != -1)
                 switch(c) {
 @@ -320,7 +322,6 @@ main(int argc, char *argv[])
         argc -= optind;
         argv += optind;

 -       initkwds();
         if (mimic_gnu)
                 setup_builtin("format", FORMATTYPE);


 which still doesn't resolve the GNU extension 'format'.

 For now, it doesn't make sense to request a GNU extension and then
 to undefine (or redefine) it. But with future multiple GNU extensions
 you may want to ask for them but also remove specific ones.

 In that case, you need to parse options twice. Once to identify
 '-g' to setup the extension and then to process the other options
 to override the setup.


From: Phil Hagelberg <phil@hagelb.org>
To: "Michael van Elst via gnats" <gnats-admin@NetBSD.org>
Cc: netbsd-bugs@netbsd.org,  gnats-bugs@netbsd.org
Subject: Re: bin/60921: m4 --undefine argument does not undefine
Date: Sun, 25 Oct 2026 09:16:36 -0700

 "Michael van Elst via gnats" <gnats-admin@NetBSD.org> writes:

 >  For now, it doesn't make sense to request a GNU extension and then
 >  to undefine (or redefine) it. But with future multiple GNU extensions
 >  you may want to ask for them but also remove specific ones.

 I don't know what this means; I'm not really concerned with GNU. I am
 just basing this off the seemingly intended behavior described in the
 NetBSD man page for m4.

 Maybe I should back up. My real purpose for reporting this bug is that I
 wish to use m4 to process existing documents that have the word "shift"
 in them. Right now NetBSD's m4 treats that word as a macro even when it
 is clearly not being used as such. When I read the manual, I saw that it
 said I should be able to undefine it so that these documents could be
 processed normally, but that is not the case.

 Looking at it more closely, there may be more to the bug. The manual
 shows that `shift' is a built-in macro which is called using parens; the
 example shows:

   shift(arg1, ...)

 Most other macros which are documented as using parens will not be invoked
 when they are merely referred to by name. For example:

   echo len | m4 # prints "len"

 For some reason, the shift macro does not work that way; any unquoted
 use of the word "shift" will invoke the macro:

   echo shift this over | m4 # prints " this over"

 So if the --undefine feature worked, that could be used as a workaround
 to this problem. But it would be even better if that weren't necessary
 and the shift macro behaved like the other macros and was not invoked
 unless called using parens.

 The word "shift" is not particularly unlikely to be found in documents
 processed by m4; this behavior makes it very easy to invoke by accident.

 Would it be better to open a separate issue for this?

From: mlelstv@serpens.de (Michael van Elst)
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/60921: m4 --undefine argument does not undefine
Date: Sun, 25 Oct 2026 23:15:04 -0000 (UTC)

 phil@hagelb.org (Phil Hagelberg) writes:

 >"Michael van Elst via gnats" <gnats-admin@NetBSD.org> writes:

 >>  For now, it doesn't make sense to request a GNU extension and then
 >>  to undefine (or redefine) it. But with future multiple GNU extensions
 >>  you may want to ask for them but also remove specific ones.

 >I don't know what this means; I'm not really concerned with GNU. I am
 >just basing this off the seemingly intended behavior described in the
 >NetBSD man page for m4.


 The option '-g' or '-G' select if a GNU extension (the 'format' builtin)
 is enabled. It's the same kind of builtin that 'dnl' is. So if you talk
 about undefining or redefining builtins, you should also consider this one.



 >Maybe I should back up. My real purpose for reporting this bug is that I
 >wish to use m4 to process existing documents that have the word "shift"
 >in them. Right now NetBSD's m4 treats that word as a macro even when it
 >is clearly not being used as such. When I read the manual, I saw that it
 >said I should be able to undefine it so that these documents could be
 >processed normally, but that is not the case.

 Indeed, and a few years ago that was possible when option processing
 wasn't yet broken.



 {1} echo "foo bar dnl bla" | m4
 foo bar {2}
 {2} echo "foo bar dnl bla" | m4 --undefine dnl
 foo bar dnl bla


 This works after applying the following patch:

 ndex: usr.bin/m4/main.c
 ===================================================================
 RCS file: /cvsroot/src/usr.bin/m4/main.c,v
 retrieving revision 1.52
 diff -p -u -r1.52 main.c
 --- usr.bin/m4/main.c   10 Jun 2026 22:25:02 -0000      1.52
 +++ usr.bin/m4/main.c   25 Oct 2026 23:13:12 -0000
 @@ -211,6 +211,8 @@ main(int argc, char *argv[])
         int n;
         int error;
         char *p;
 +       int save_gnu;
 +       int save_prefix;

         setprogname(argv[0]);

 @@ -228,6 +230,38 @@ main(int argc, char *argv[])
         outfile = NULL;
         resizedivs(MAXOUT);

 +       save_gnu = mimic_gnu;
 +       save_prefix = prefix_builtins;
 +
 +       /*
 +        * We need to know if -P, -g or -G is there before checking -D and -U.
 +        */
 +       while ((c = getopt_long(argc, argv, "D:d:EF:GgI:iL:o:PR:Qst:U:v",
 +           longopts, NULL)) != -1) {
 +               switch(c) {
 +               case 'G':
 +                       mimic_gnu = 0;
 +                       break;
 +               case 'g':
 +                       mimic_gnu = 1;
 +                       break;
 +               case 'P':
 +                       prefix_builtins = 1;
 +                       break;
 +               }
 +       }
 +       optind = 1;
 +
 +       initkwds();
 +       if (mimic_gnu)
 +               setup_builtin("format", FORMATTYPE);
 +
 +       /*
 +        * reset options
 +        */
 +       mimic_gnu = save_gnu;
 +       prefix_builtins = save_prefix;
 +
         while ((c = getopt_long(argc, argv, "D:d:EF:GgI:iL:o:PR:Qst:U:v",
             longopts, NULL)) != -1)
                 switch(c) {
 @@ -320,10 +354,6 @@ main(int argc, char *argv[])
         argc -= optind;
         argv += optind;

 -       initkwds();
 -       if (mimic_gnu)
 -               setup_builtin("format", FORMATTYPE);
 -
         active = stdout;                /* default active output     */
         bbase[0] = bufbase;

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.