Skip to content

Commit 1c9895b

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 Signed-off-by: Simon Wollwage <[email protected]>
1 parent 3f10e59 commit 1c9895b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/libc/stdlib/getopt.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +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;
116-
else if (nargc > ++optind)
115+
if (nargc > (optind + 1) && nargv[optind + 1][0] != '-') {
116+
optarg = nargv[++optind];
117+
} else {
118+
optarg = NULL;
119+
}
120+
} else if (nargc > ++optind)
117121
optarg = nargv[optind];
118122
else {
119123
/* option-argument absent */

0 commit comments

Comments
 (0)