This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/11040] New: getopt mistakenly allows '-;' as short option
- From: "ebb9 at byu dot net" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 1 Dec 2009 16:50:54 -0000
- Subject: [Bug libc/11040] New: getopt mistakenly allows '-;' as short option
- Reply-to: sourceware-bugzilla at sourceware dot org
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:'.
--
Summary: getopt mistakenly allows '-;' as short option
Product: glibc
Version: 2.11
Status: NEW
Severity: normal
Priority: P2
Component: libc
AssignedTo: drepper at redhat dot com
ReportedBy: ebb9 at byu dot net
CC: glibc-bugs at sources dot redhat dot com
http://sourceware.org/bugzilla/show_bug.cgi?id=11040
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.