public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/11040] New: getopt mistakenly allows '-;' as short option
@ 2009-12-01 16:51 ebb9 at byu dot net
  2009-12-01 16:54 ` [Bug libc/11040] " ebb9 at byu dot net
  2010-04-08  0:31 ` drepper at redhat dot com
  0 siblings, 2 replies; 4+ messages in thread
From: ebb9 at byu dot net @ 2009-12-01 16:51 UTC (permalink / raw)
  To: glibc-bugs

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.


^ permalink raw reply	[flat|nested] 4+ messages in thread
[parent not found: <bug-11040-131@http.sourceware.org/bugzilla/>]

end of thread, other threads:[~2014-06-30 20:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-01 16:51 [Bug libc/11040] New: getopt mistakenly allows '-;' as short option ebb9 at byu dot net
2009-12-01 16:54 ` [Bug libc/11040] " ebb9 at byu dot net
2010-04-08  0:31 ` drepper at redhat dot com
     [not found] <bug-11040-131@http.sourceware.org/bugzilla/>
2014-06-30 20:36 ` fweimer at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).