Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date: Tue, 21 Oct 2014 07:35:39 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: [PATCH] getopt: fix optional argument processing

On Tue, Oct 21, 2014 at 01:03:28PM +0200, Felix Fietkau wrote:
> Processing an option character with optional argument fails if the
> option is last on the command line. This happens because the
> if (optind >= argc) check runs first before testing for optional
> argument.
> ---
>  src/misc/getopt.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/src/misc/getopt.c b/src/misc/getopt.c
> index 8a2e4d5..7ce3789 100644
> --- a/src/misc/getopt.c
> +++ b/src/misc/getopt.c
> @@ -55,7 +55,9 @@ int getopt(int argc, char * const argv[], const char *optstring)
>  		return '?';
>  	}
>  	if (optstring[i+1] == ':') {
> +		if (optstring[i+2] == ':') optarg = 0;
>  		if (optind >= argc) {
> +			if (optstring[i+2] == ':') return c;

Couldn't this be:

		if (optstring[i+2] == ':') optarg = 0;
		else if (optind >= argc) {

I'm not sure if that's nicer but it eliminates the additional return
path you're adding.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.