[sheepdog] [PATCH] collie: correct command-line options parser

MORITA Kazutaka morita.kazutaka at gmail.com
Mon Mar 18 01:43:24 CET 2013


At Fri, 15 Mar 2013 16:46:59 +0800,
Liu Yuan wrote:
> 
> On 03/15/2013 02:00 PM, ChenZhe wrote:
> > From: ChenZhe <chenzhe at xinnet.com>
> > 
> > "collie cluster recover disable -p7001" not work.
> > getopt_long() will reorder the options in argv.
> > so, we need to skip one more argument before getopt() for the subcmd who need third arg.
> > 
> > Signed-off-by: ChenZhe <chenzhe at xinnet.com>
> > Signed-off-by: ChenZhe <cz at de3eb.cn>
> > ---
> >  collie/collie.c |    5 ++++-
> >  1 files changed, 4 insertions(+), 1 deletions(-)
> > 
> > diff --git a/collie/collie.c b/collie/collie.c
> > index 08c78eb..2d05f3d 100644
> > --- a/collie/collie.c
> > +++ b/collie/collie.c
> > @@ -348,6 +348,9 @@ int main(int argc, char **argv)
> >  	long_options = build_long_options(sd_opts);
> >  	short_options = build_short_options(sd_opts);
> >  
> > +	if(flags & SUBCMD_FLAG_NEED_THIRD_ARG && argc > 3)
> > +		optind++;
> > +
> >  	while ((ch = getopt_long(argc, argv, short_options, long_options,
> >  				&longindex)) >= 0) {
> >  
> > @@ -391,7 +394,7 @@ int main(int argc, char **argv)
> >  		}
> >  	}
> >  
> > -	if (flags & SUBCMD_FLAG_NEED_THIRD_ARG && argc == optind)
> 
> Kazutaka, what does argc == optind mean?

It checks whether all the arguments are consumed or not.

> 
> > +	if (flags & SUBCMD_FLAG_NEED_THIRD_ARG && optind <= 3)
> >  		subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> >  
> >  	return command_fn(argc, argv);
> > 
> 
> Kazutaka, this bug does exist (even with '-p 7001'), but 'optind <= 3'
> looks quite suspicious to me, could you review it?

It is not a good idea to increase optind before getopt_long.  The real
problem is that we specify the thrid arguemnt as argv[3] in
cluster_recover.  It should be argv[optind].

Thanks,

Kazutaka



More information about the sheepdog mailing list