Bug 291374 - getopt(3) GNU extension wrong behavior
Summary: getopt(3) GNU extension wrong behavior
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 14.3-STABLE
Hardware: Any Any
: --- Affects Some People
Assignee: Warner Losh
URL: https://github.com/freebsd/freebsd-sr...
Keywords:
Depends on:
Blocks:
 
Reported: 2025-12-03 19:39 UTC by Eugene Grosbein
Modified: 2026-07-19 09:21 UTC (History)
5 users (show)

See Also:
eugen: mfc-stable15?
eugen: mfc-stable14?


Attachments
Patch for getopt manpage (2.08 KB, patch)
2025-12-29 12:59 UTC, Simon Wollwage
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Eugene Grosbein freebsd_committer freebsd_triage 2025-12-03 19:39:26 UTC
From the getopt(3) manual page:

     The option string optstring may contain the following elements:
     individual characters, and characters followed by a colon to indicate an
     option argument is to follow.  If an individual character is followed by
     two colons, then the option argument is optional; optarg is set to the
     rest of the current argv word, or NULL if there were no more characters
     in the current word.  This is a GNU extension.  For example, an option
     string "x" recognizes an option "-x", and an option string "x:"
     recognizes an option and argument "-x argument".  It does not matter to
     getopt() if a following argument has leading white space.

Note the last clause about leading white space. It does not match the code.
For example, our date(1) utility uses getopt(argc, argv, "f:I::jnRr:uv:z:") and look at this:

$ date -Idate
2025-12-04
$ date -I date
date: illegal time format
usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]
            [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
            [[[[[[cc]yy]mm]dd]HH]MM[.SS] | new_date] [+output_fmt]

But this is not all. Consider the following simple test programm:

#include <stdio.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    int         ch;

    while ((ch = getopt(argc, argv, "cCd::F:lnoOp:s:u:U:wW")) != -1) {
        printf("ch is %c optarg is %s\n",
            (char)ch, optarg ? optarg : " NULL");
    }

    return (0);
}

Option list taken from our tftpd(8) programm that uses "-s path" for a chroot and optional "-d value" for debugging.

Build it: cc -o getoptest -ansi -pedantic -Wall getoptest.c
Run it:

$ ./getoptest -l -w -d14 -s /usr/local/admin/tftproot -u admin -U 0
ch is l optarg is  NULL
ch is w optarg is  NULL
ch is d optarg is 14
ch is s optarg is /usr/local/admin/tftproot
ch is u optarg is admin
ch is U optarg is 0

So far, so good. And now:

$ ./getoptest -l -w -d 14 -s /usr/local/admin/tftproot -u admin -U 0
ch is l optarg is  NULL
ch is w optarg is  NULL
ch is d optarg is  NULL

Option list silently truncated. No -s, no chrooting for tftpd.
Comment 1 Eugene Grosbein freebsd_committer freebsd_triage 2025-12-03 19:53:13 UTC
Same wrong behavior verified with 14.3, 13.5 and 12.4 versions. In FreeBSD 8.4 the getopt did not support this "double colon" GNU Extension and I assume same bug was here since 9.0.
Comment 2 Jose Luis Duran freebsd_committer freebsd_triage 2025-12-04 03:13:39 UTC
(In reply to Eugene Grosbein from comment #1)

The bug also appears to be present on NetBSD, which is where getopt(3) originates.
Comment 3 Simon Wollwage freebsd_triage 2025-12-04 11:33:01 UTC
(In reply to Jose Luis Duran from comment #2)

I think I found the issue.

It's in this part:

else if (oli[2] == ':')
	/*
	 * GNU Extension, for optional arguments if the rest of
	 * the argument is empty, we return NULL
	 */
	optarg = NULL;

So, if place is NULL (because of the space), it just sets the arg to NULL. Next iteration it would see just the 14 as an option and fails. 

Changing it to something like this should fix it:

else if (oli[2] == ':') {
	/*
	 * GNU Extension, for optional arguments if the rest of
	 * the argument is empty, we return NULL
	 */
	if (nargc > optind + 1 && nargv[optind + 1][0] != '-') {
	optarg = nargv[++optind];
    } else {
        optarg = NULL;
    }
}

But I am new to this and am not sure how to compile the stdlib part and test it. If I could get some guidance on that part I would be happy to make my first contribution.
Comment 4 Jose Luis Duran freebsd_committer freebsd_triage 2025-12-04 21:02:18 UTC
(In reply to Simon Wollwage from comment #3)

Yes, thank you!  That patch could work.

You should be able to just make libc (make -C lib/libc), however, that never works for me, so I always buildworld:

1. cd /usr/src       (or to the directory containing your source tree).
2. make -j $(nproc) buildworld
3. make -j $(nproc) installworld

Also, since there are no tests for getopt(3), our tests are all the other tests that use getopt(3).  To test the entire suite I've been using bricoler (https://github.com/markjdb/bricoler).  I realize that for this particular bug, the barrier for testing might be particularly high.

There are a few style(9) changes, but overall that seems to fix this bug.  If you're up for it, open a GiHub Pull Request OR a review on Phabricator (https://reviews.freebsd.org) (I'm sure these instructions are somewhere, but I can't seem to find them).

Finally, given the original file was obtained from NetBSD, it is always a good practice to also file a bug with them, letting them know this bug report and the fix.
Comment 5 Simon Wollwage freebsd_triage 2025-12-05 14:23:35 UTC
(In reply to Jose Luis Duran from comment #4)

Thank you. I'll work on it. make buildworld takes some time. 

In terms of formatting, I did install the emacs plugin, but the new style is very different from the existing source file. The fix is minor in scope so I guess I'll stick to the existing formatting.

Once the patch is accepted I will of course open the same for NetBSD. 

Thank you for the tip with bricoler, I was unaware of that.
Comment 6 Simon Wollwage freebsd_triage 2025-12-06 13:03:25 UTC
Pull rewuest created here https://github.com/freebsd/freebsd-src/pull/1918
Comment 7 Simon Wollwage freebsd_triage 2025-12-10 13:40:31 UTC
There is currently some discussion about the wording of the manpage and it's interpretation on the NetBSD mailiing list. See https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=59828
Comment 8 Simon Wollwage freebsd_triage 2025-12-12 06:08:27 UTC
New pull request for manpage update instead of changing the code, as discussed in https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=59828:

 https://github.com/freebsd/freebsd-src/pull/1923
Comment 9 Simon Wollwage freebsd_triage 2025-12-29 12:59:09 UTC
Created attachment 266620 [details]
Patch for getopt manpage

As Github seems to have no movement on reviews I'll also attach a patch here.
Comment 10 commit-hook freebsd_committer freebsd_triage 2026-01-09 21:03:49 UTC
A commit in branch main references this bug:

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

commit 4b2a8aab43ff747bcba6c28f5c0431141ec198a0
Author:     Simon Wollwage <rootnode+freebsd@wollwage.com>
AuthorDate: 2025-12-12 05:11:40 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2026-01-09 21:02:23 +0000

    getopt(3): be more explicit about :: extension

    Make it possible to search for literal two colons (::) and actually
    find something.  Make the "x"/"x:"/"x::" examples more explicit and
    more visibile.

    Signed-off-by: Simon Wollwage <rootnode+freebsd@wollwage.com>
    Obtained from:  NetBSD, nbuwe <uwe@stderr.spb.ru>, 856d5b6
    PR: 291374
    Reviewed by: imp, jlduran
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1923

 lib/libc/stdlib/getopt.3 | 49 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 15 deletions(-)
Comment 11 Eugene Grosbein freebsd_committer freebsd_triage 2026-01-11 07:44:48 UTC
(In reply to Simon Wollwage from comment #8)

While some improvement of wording in the man page is good, silent truncation of option list shown in this PR is not. The code should be fixed somehow.
Comment 12 Simon Wollwage freebsd_triage 2026-01-11 11:53:03 UTC
(In reply to Eugene Grosbein from comment #11)

It's not really silent though. It does return -1 if there is a space, e.g. "-d 14" in the original example.
Comment 13 Eugene Grosbein freebsd_committer freebsd_triage 2026-01-11 12:44:13 UTC
(In reply to Simon Wollwage from comment #12)

Well, for user side it's silent because tftpd (or other program) just stops processing option list after such space character.
Comment 14 Simon Wollwage freebsd_triage 2026-01-11 16:59:02 UTC
(In reply to Eugene Grosbein from comment #13)

But tricky. Take the '-d' option on tftpd. If space would be allowed, say you call it with 'tftpd -d 4'. Is 4 now the value given to the debug option or is it a directory name?

'tftpd' cannot use 'optind < argc' to check if anything was missing.

So, adding the space handling seems difficult. Detecting if the list has been exhausted is also difficult. I think getopt returning '-1' here is unfortunately correct.
Comment 15 Siva Mahadevan freebsd_committer freebsd_triage 2026-01-11 20:36:30 UTC
(In reply to commit-hook from comment #10)
This commit seems to break a couple etcupdate tests in CI: https://ci.freebsd.org/view/Test/job/FreeBSD-main-amd64-test/27595/testReport/

I've bisected to this commit, @Simon can you take a look?

You can repro the failure locally with Bricoler:

$ bricoler run freebsd-src-regression-suite --param freebsd-src:url=/usr/src --param freebsd-src:branch= --param freebsd-src-regression-suite:hypervisor=bhyve --param freebsd-src-regression-suite:memory=4096 --param freebsd-src-regression-suite:ncpus=2 --param freebsd-src-regression-suite:parallelism=1 --param freebsd-src-regression-suite:count=100 --param freebsd-src-regression-suite:tests='usr.sbin/etcupdate'
Comment 16 Siva Mahadevan freebsd_committer freebsd_triage 2026-01-11 20:48:22 UTC
(In reply to Siva Mahadevan from comment #15)
Sorry about that, please disregard this. I had the wrong commit ID in my bisection.
Comment 17 Jose Luis Duran freebsd_committer freebsd_triage 2026-05-30 20:28:41 UTC
I think this bug should be closed as work as expected now. Maybe it's just missing the MFCs?
Comment 18 Eugene Grosbein freebsd_committer freebsd_triage 2026-06-01 06:03:06 UTC
(In reply to Jose Luis Duran from comment #17)

No changes were made to 14.4-STABLE.