mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH v2] getopt: fix optional argument processing
@ 2014-10-21 20:24 Felix Fietkau
  2014-11-16  4:46 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Felix Fietkau @ 2014-10-21 20:24 UTC (permalink / raw)
  To: musl

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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/misc/getopt.c b/src/misc/getopt.c
index 8a2e4d5..f94c4f7 100644
--- a/src/misc/getopt.c
+++ b/src/misc/getopt.c
@@ -55,7 +55,8 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		return '?';
 	}
 	if (optstring[i+1] == ':') {
-		if (optind >= argc) {
+		if (optstring[i+2] == ':') optarg = 0;
+		else if (optind >= argc) {
 			if (optstring[0] == ':') return ':';
 			if (opterr) {
 				write(2, argv[0], strlen(argv[0]));
@@ -65,7 +66,6 @@ int getopt(int argc, char * const argv[], const char *optstring)
 			}
 			return '?';
 		}
-		if (optstring[i+2] == ':') optarg = 0;
 		if (optstring[i+2] != ':' || optpos) {
 			optarg = argv[optind++] + optpos;
 			optpos = 0;
-- 
2.0.4



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

* Re: [PATCH v2] getopt: fix optional argument processing
  2014-10-21 20:24 [PATCH v2] getopt: fix optional argument processing Felix Fietkau
@ 2014-11-16  4:46 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2014-11-16  4:46 UTC (permalink / raw)
  To: musl

On Tue, Oct 21, 2014 at 10:24:50PM +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.
> ---

Thanks, committed.

Rich


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

end of thread, other threads:[~2014-11-16  4:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-21 20:24 [PATCH v2] getopt: fix optional argument processing Felix Fietkau
2014-11-16  4:46 ` Rich Felker

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