diff options
author | Rich Felker <dalias@aerifal.cx> | 2018-08-22 19:29:56 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2018-08-22 19:29:56 -0400 |
commit | 6aeb131b4c1aee30d7f7125a8a88ce6f41172f01 (patch) | |
tree | cf39f9b7af03f4525a4aa297317bbcf754891116 /src | |
parent | 184ef36f85d66583306413727fc5f39557d391e4 (diff) | |
download | musl-6aeb131b4c1aee30d7f7125a8a88ce6f41172f01.tar.gz musl-6aeb131b4c1aee30d7f7125a8a88ce6f41172f01.tar.bz2 musl-6aeb131b4c1aee30d7f7125a8a88ce6f41172f01.tar.xz musl-6aeb131b4c1aee30d7f7125a8a88ce6f41172f01.zip |
getopt: update optarg and optind correctly on missing argument
the text of the specification for getopt's handling of options that
require an argument, which requires updating optarg and optind, does
not exclude the error case where the end of the argument list has been
reached. in that case, it is expected that optarg be assigned
argv[argc] (normally null) and optind be incremented by 2, resulting
in a value of argc+1.
Diffstat (limited to 'src')
-rw-r--r-- | src/misc/getopt.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/misc/getopt.c b/src/misc/getopt.c index e921a60e..cd1f292f 100644 --- a/src/misc/getopt.c +++ b/src/misc/getopt.c @@ -84,8 +84,12 @@ int getopt(int argc, char * const argv[], const char *optstring) return '?'; } if (optstring[i] == ':') { - if (optstring[i+1] == ':') optarg = 0; - else if (optind >= argc) { + optarg = 0; + if (optstring[i+1] != ':' || optpos) { + optarg = argv[optind++] + optpos; + optpos = 0; + } + if (optind > argc) { optopt = c; if (optstring[0] == ':') return ':'; if (opterr) __getopt_msg(argv[0], @@ -93,10 +97,6 @@ int getopt(int argc, char * const argv[], const char *optstring) optchar, k); return '?'; } - if (optstring[i+1] != ':' || optpos) { - optarg = argv[optind++] + optpos; - optpos = 0; - } } return c; } |