From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10648 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: getopt_long_only Date: Thu, 20 Oct 2016 12:17:43 -0400 Message-ID: <20161020161743.GR19318@brightrain.aerifal.cx> References: <002301d22560$0d79a610$286cf230$@codeaurora.org> <20161013184019.GV19318@brightrain.aerifal.cx> <20161020055400.GO19318@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1476980305 21564 195.159.176.226 (20 Oct 2016 16:18:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 20 Oct 2016 16:18:25 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-10661-gllmg-musl=m.gmane.org@lists.openwall.com Thu Oct 20 18:18:20 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1bxG2X-0002hX-Ms for gllmg-musl@m.gmane.org; Thu, 20 Oct 2016 18:17:57 +0200 Original-Received: (qmail 7519 invoked by uid 550); 20 Oct 2016 16:17:56 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 7498 invoked from network); 20 Oct 2016 16:17:56 -0000 Content-Disposition: inline In-Reply-To: <20161020055400.GO19318@brightrain.aerifal.cx> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:10648 Archived-At: On Thu, Oct 20, 2016 at 01:54:00AM -0400, Rich Felker wrote: > On Thu, Oct 13, 2016 at 02:40:19PM -0400, Rich Felker wrote: > > On Thu, Oct 13, 2016 at 01:43:28PM -0400, Daniel Sabogal wrote: > > > On Thu, Oct 13, 2016 at 10:42 AM, Sidney Manning wrote: > > > > I may have found an issue with this API. When a double hyphen is > > > > encountered the API is supposed to return a -1 but I'm seeing a '?'. > > > > > > > > My reference is from the Solaris docs which say: "The special option "--" > > > > can be used to delimit the end of the options; when it is encountered, -1 is > > > > returned and "--" is skipped. I attached a test case to show the problem. > > > > > > > > I made the following change it will cause the code to punt to getopt when > > > > just a "--" is found: > > > > argv[optind][0] == '-' > > > > argv[optind][1] == '-' > > > > argv[optind][2] == NULL > > > > > > > > diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c > > > > index 480c001..9764f56 100644 > > > > --- a/src/misc/getopt_long.c > > > > +++ b/src/misc/getopt_long.c > > > > @@ -53,7 +53,7 @@ static int __getopt_long_core(int argc, char *const *argv, > > > > con > > > > { > > > > optarg = 0; > > > > if (longopts && argv[optind][0] == '-' && > > > > - ((longonly && argv[optind][1]) || > > > > + ((longonly && argv[optind][1]) && > > > > (argv[optind][1] == '-' && argv[optind][2]))) > > > > { > > > > int colon = > > > > optstring[optstring[0]=='+'||optstring[0]=='-']==':' > > > > > > This will break getopt_long() since the entire condition won't hold > > > whenever !longonly. > > > > > > I think just changing the longonly case to the following is sufficient. > > > > > > (longonly && argv[optind][1] && argv[optind][1] != '-') > > > > Yes, that looks like the correct fix. > > Has anyone tested and confirmed this? I tested and it fixes the testcase, seems correct, and is in a code path where it only affects getopt_long_only, so I think it's safe to make this change. Committing a patch. Rich