public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ebb9 at byu dot net" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sources.redhat.com
Subject: [Bug libc/11039] New: getopt mishandles optstring of "+:"
Date: Tue, 01 Dec 2009 16:28:00 -0000	[thread overview]
Message-ID: <20091201162750.11039.ebb9@byu.net> (raw)

According to the getopt man pages, for example:

http://www.kernel.org/doc/man-pages/online/pages/man3/getopt.3.html

If the first character (following any optional '+' or '-' described above) of
optstring is a colon (':'), then getopt() returns ':' instead of '?' to indicate
a missing option argument.

However, the code in getopt.c does not consistently follow this rule.

$ cat foo.c
#include <unistd.h>
#include <stdio.h>
int
main (int argc, char **argv)
{
  int c = getopt (argc, argv, "+:a:b");
  if (c == -1)
    puts ("got -1");
  else
    printf ("got %c\n", c);
  c = getopt (argc, argv, "+:a:b");
  if (c == -1)
    puts ("got -1");
  else
    printf ("got %c\n", c);
  return 0;
}
$ ./foo -a
./foo2: option requires an argument -- a
got :
got -1
$ ./foo -b -a
got b
./foo2: option requires an argument -- a
got ?

Expected behavior - stderr should be silent, and the -a without argument should
return ':', not '?'.

Workaround: code can set opterr=0 before the first getopt{,_long} call (to
silence the spurious warning if argv[1] is missing an argument), then leave off
the leading '-' or '+' of opstring for all subsequent calls for a given
argc/argv parse (to silence the warning and return ':' instead of '?' if any
subsequent argv is missing an argument).

-- 
           Summary: getopt mishandles optstring of "+:"
           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=11039

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


             reply	other threads:[~2009-12-01 16:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-01 16:28 ebb9 at byu dot net [this message]
2009-12-01 16:32 ` [Bug libc/11039] " ebb9 at byu dot net
2009-12-02 22:17 ` ebb9 at byu dot net
2010-04-08  0:57 ` drepper at redhat dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20091201162750.11039.ebb9@byu.net \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).