NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/59443: getopt/getopt_long prints errors when +/- is present before :



>Number:         59443
>Category:       lib
>Synopsis:       getopt/getopt_long prints errors when +/- is present before :
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun May 25 18:30:00 +0000 2025
>Originator:     Haelwenn (lanodan) Monnier
>Release:        10.0
>Organization:
>Environment:
NetBSD build 10.0 NetBSD 10.0 (GENERIC) #0: Thu Mar 28 08:33:33 UTC 2024  mkrepro%mkrepro.NetBSD.org@localhost:/usr/src/sys/arch/amd64/compile/GENERIC amd64

>Description:
When optstring contains a leading + or -, getopt(3)/getopt_long(3) ignores the immediately following : and prints errors.
>How-To-Repeat:
$ cat getopt_test.c
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int
main(int argc, char *argv[])
{
        char *optstr = getenv("OPTSTR");

        int c = -1;
        while((c = getopt(argc, argv, optstr)) != -1)
        {
                /* blank */
        }

        return 0;
}
$ clang -std=c99 -g -O2 -Wall -Wextra -o getopt_test getopt_test.c
$ OPTSTR=":" ./getopt_test -f
$ OPTSTR="+:" ./getopt_test -f
getopt_test: unknown option -- f
$ OPTSTR="-:" ./getopt_test -f
getopt_test: unknown option -- f

$ cat getopt_long_test.c
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int
main(int argc, char *argv[])
{
        char *optstr = getenv("OPTSTR");

        static struct option opts[] = {
                {0, 0, 0, 0},
        };

        int c = -1;
        while((c = getopt_long(argc, argv, optstr, opts, NULL)) != -1)
        {
                /* blank */
        }

        return 0;
}
$ clang -std=c99 -g -O2 -Wall -Wextra -o getopt_long_test getopt_long_test.c
$ OPTSTR=":" ./getopt_long_test -f
$ OPTSTR="+:" ./getopt_long_test -f
getopt_long_test: unknown option -- f
$ OPTSTR="-:" ./getopt_long_test -f 
getopt_long_test: unknown option -- f
$ OPTSTR=":" ./getopt_long_test --foobar 
$ OPTSTR="+:" ./getopt_long_test --foobar
getopt_long_test: unknown option -- foobar
$ OPTSTR="-:" ./getopt_long_test --foobar 
getopt_long_test: unknown option -- foobar
>Fix:



Home | Main Index | Thread Index | Old Index