getopt_long is documented as accepting an optstring of "W;" as an extension that parses '-W longopt=value' the same as '--longopt=value'. However, using this extension also makes apps mistakenly accept '-;' as a valid short option. In practice, encountering '-;' as a short option will be rare (since it requires shell quoting). Furthermore, if ';' appears in optstring outside of the documented "W;" extension, there is no reason to forbid it from being a valid short option as an extension permitted by POSIX. However, if the only ';' in optstring immediately follows 'W', it makes more sense to reject ';', the same way that ':' is rejected. And coding-wise, it is easier to forbid all use of ';', rather than making the code more complicated to determine whether ';' in optstring immediately follows 'W'. $ cat foo.c #include <unistd.h> #include <stdio.h> #include <getopt.h> static const struct option opts[] = { { "alpha", no_argument, NULL, 'a' }, { "beta", required_argument, NULL, 'b' }, { NULL, 0, NULL, 0 } }; int main (int argc, char **argv) { int c = getopt_long (argc, argv, "ab:W;", opts, NULL); if (c == -1) puts ("got -1"); else printf ("got %c\n", c); c = getopt_long (argc, argv, "ab:W;", opts, NULL); if (c == -1) puts ("got -1"); else printf ("got %c\n", c); return 0; } $ ./foo '-a;' got a got ; $ ./foo2 '-a:' got a ./foo2: invalid option -- : got ? Workaround: code using the "W;" extension must be prepared to deal with a return value of ';' and manually handle it as an invalid option. Expected results: '-a;' should have errored out like '-a:'.
Created attachment 4436 [details] patch 2009-12-01 Eric Blake <ebb9@byu.net> * posix/getopt.c (_getopt_internal_r): Reject '-;' as short option, since it conflicts with "W;" optstring extension.
Fixed in git.