diff --git a/lib/libc/stdlib/getopt.c b/lib/libc/stdlib/getopt.c index 2b5e3fa6903256..d2dc35bf8c613e 100644 --- a/lib/libc/stdlib/getopt.c +++ b/lib/libc/stdlib/getopt.c @@ -107,13 +107,17 @@ getopt(int nargc, char * const nargv[], const char *ostr) entire next argument. */ if (*place) optarg = place; - else if (oli[2] == ':') + else if (oli[2] == ':') { /* * GNU Extension, for optional arguments if the rest of * the argument is empty, we return NULL */ - optarg = NULL; - else if (nargc > ++optind) + if (nargc > (optind + 1) && nargv[optind + 1][0] != '-') { + optarg = nargv[++optind]; + } else { + optarg = NULL; + } + } else if (nargc > ++optind) optarg = nargv[optind]; else { /* option-argument absent */