mailing list of musl libc
 help / color / mirror / code / Atom feed
* getopt_long_only
@ 2016-10-13 14:42 Sidney Manning
  2016-10-13 17:43 ` getopt_long_only Daniel Sabogal
  0 siblings, 1 reply; 6+ messages in thread
From: Sidney Manning @ 2016-10-13 14:42 UTC (permalink / raw)
  To: musl

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

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]=='-']==':'


Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by
The Linux Foundation


[-- Attachment #2: getopt.c --]
[-- Type: text/plain, Size: 963 bytes --]

#define _GNU_SOURCE
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>

struct option options[] =
{
  {
    "stop-at-entry", 0, 0, 's'
  },
  {
    "shell", 1, 0, 'c'
  },
  {
    NULL, 0, 0, 0
  }
};

extern int optreset;
int main(void)
{
  int val;
  optind = 1;
  //optreset = 1;

  int argc = 3;
  char *argv[] = {"dummy_arg", "--shell=/bin/sh", "--", NULL};
  int longindex = -1;
  char *optstring = "sc::";

  printf("**optind is %d, argv[%d] is %s\n", optind, optind, argv[optind]);
  val = getopt_long_only(argc, argv, optstring, &options[0], &longindex);
  if (val != 'c') {
    printf ("%d: getopt_long_only FAIL\n", __LINE__);
    return 1;
  }
  printf("**optind is %d, argv[%d] is %s\n", optind, optind, argv[optind]);
  val = getopt_long_only(argc, argv, optstring, &options[0], &longindex);
  if (val != -1) {
    printf ("%d: getopt_long_only FAIL\n", __LINE__);
    return 1;
  }
  return 0;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: getopt_long_only
  2016-10-13 14:42 getopt_long_only Sidney Manning
@ 2016-10-13 17:43 ` Daniel Sabogal
  2016-10-13 18:40   ` getopt_long_only Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Sabogal @ 2016-10-13 17:43 UTC (permalink / raw)
  To: musl

On Thu, Oct 13, 2016 at 10:42 AM, Sidney Manning <sidneym@codeaurora.org> 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] != '-')


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: getopt_long_only
  2016-10-13 17:43 ` getopt_long_only Daniel Sabogal
@ 2016-10-13 18:40   ` Rich Felker
  2016-10-20  5:54     ` getopt_long_only Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Rich Felker @ 2016-10-13 18:40 UTC (permalink / raw)
  To: musl

On Thu, Oct 13, 2016 at 01:43:28PM -0400, Daniel Sabogal wrote:
> On Thu, Oct 13, 2016 at 10:42 AM, Sidney Manning <sidneym@codeaurora.org> 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.

Rich


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: getopt_long_only
  2016-10-13 18:40   ` getopt_long_only Rich Felker
@ 2016-10-20  5:54     ` Rich Felker
  2016-10-20 16:17       ` getopt_long_only Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Rich Felker @ 2016-10-20  5:54 UTC (permalink / raw)
  To: musl

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 <sidneym@codeaurora.org> 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?

Rich


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: getopt_long_only
  2016-10-20  5:54     ` getopt_long_only Rich Felker
@ 2016-10-20 16:17       ` Rich Felker
  0 siblings, 0 replies; 6+ messages in thread
From: Rich Felker @ 2016-10-20 16:17 UTC (permalink / raw)
  To: musl

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 <sidneym@codeaurora.org> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: getopt_long_only
@ 2016-10-13 18:03 Sidney Manning
  0 siblings, 0 replies; 6+ messages in thread
From: Sidney Manning @ 2016-10-13 18:03 UTC (permalink / raw)
  To: musl

> -----Original Message-----
> From: Sidney Manning [mailto:sidneym@codeaurora.org]
> Sent: Thursday, October 13, 2016 9:43 AM
> To: 'musl@lists.openwall.com' <musl@lists.openwall.com>
> Subject: getopt_long_only
> 
> 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]) &&

It was pointed out to me that, "--" is a special case and should be treated
as such (and the above change is wrong)
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -52,6 +52,10 @@ static int __getopt_long(int argc, char *const *argv,
const char *optstring, con
 static int __getopt_long_core(int argc, char *const *argv, const char
*optstring, const struct option *longopts, int *idx, int longonly)
 {
        optarg = 0;
+       if (longopts && argv[optind][0] == '-' && argv[optind][1] == '-' &&
+            !argv[optind][2])
+               return getopt(argc, argv, optstring);
+
        if (longopts && argv[optind][0] == '-' &&
                ((longonly && argv[optind][1]) ||
                 (argv[optind][1] == '-' && argv[optind][2])))


 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-10-20 16:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-13 14:42 getopt_long_only Sidney Manning
2016-10-13 17:43 ` getopt_long_only Daniel Sabogal
2016-10-13 18:40   ` getopt_long_only Rich Felker
2016-10-20  5:54     ` getopt_long_only Rich Felker
2016-10-20 16:17       ` getopt_long_only Rich Felker
2016-10-13 18:03 getopt_long_only Sidney Manning

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).