Skip to content

Commit f8bc751

Browse files
author
Simon Wollwage
committed
lib/libc: fix bug in parsing in getopt
getopt had an issue when parsing optional arguments if they had a space between the opion and its value. Request: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=291374
1 parent d20da5c commit f8bc751

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/libc/stdlib/getopt.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,17 @@ getopt(int nargc, char * const nargv[], const char *ostr)
107107
entire next argument. */
108108
if (*place)
109109
optarg = place;
110-
else if (oli[2] == ':')
110+
else if (oli[2] == ':') {
111111
/*
112112
* GNU Extension, for optional arguments if the rest of
113113
* the argument is empty, we return NULL
114114
*/
115-
optarg = NULL;
115+
if (nargc > (optind + 1)) {
116+
optarg = nargv[++optind];
117+
} else {
118+
optarg = NULL;
119+
}
120+
}
116121
else if (nargc > ++optind)
117122
optarg = nargv[optind];
118123
else {

0 commit comments

Comments
 (0)