mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: test-getopt-posix fails on musl
Date: Sat, 24 Feb 2018 10:09:18 -0500	[thread overview]
Message-ID: <20180224150918.GL1436@brightrain.aerifal.cx> (raw)
In-Reply-To: <1714550.SfedhZ9LKS@omega>

[-- Attachment #1: Type: text/plain, Size: 1204 bytes --]

On Sat, Feb 24, 2018 at 10:31:55AM +0100, Bruno Haible wrote:
> Hi,
> 
> On Alpine Linux 3.7.0, which uses musl libc, the gnulib test 'test-getopt-posix'
> fails. To me, this looks like a POSIX compliance bug of musl.
> Find attached a reduced test case.
> 
> $ gcc foo.c -Wall
> $ ./a.ou	t
> foo.c:134: assertion 'options[0] == ':' || ((options[0] == '-' || options[0] == '+') && options[1] == ':')' failed
> 
> Reference: POSIX
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html

It seems like the behavior you're claiming is wrong is that getopt is
treating ':' in the optstring as a valid option rather than just a
modifier to the option character before it or the special flag at the
beginning of optstring. The likely reason the bug exists is that the
spec isn't clear that colons can't be option characters (maybe they
could be, especially if they're not initial or immediately following
another option character?) but it is clear that characters outside
class alnum are not required to be supported as options, and I think
it's clear that accepting -: is not the right thing to do here.

Attached patch should fix it; if nobody objects right away I'll apply.

Thanks!

Rich


[-- Attachment #2: getopt-colon.diff --]
[-- Type: text/plain, Size: 430 bytes --]

diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index e9bab41..e921a60 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -77,7 +77,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		if (l>0) i+=l; else i++;
 	} while (l && d != c);
 
-	if (d != c) {
+	if (d != c || c == ':') {
 		optopt = c;
 		if (optstring[0] != ':' && opterr)
 			__getopt_msg(argv[0], ": unrecognized option: ", optchar, k);

      reply	other threads:[~2018-02-24 15:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-24  9:31 Bruno Haible
2018-02-24 15:09 ` Rich Felker [this message]

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=20180224150918.GL1436@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).