From 35025a9f1616b8675ebc72c8f94a5ab1dcfe8a4f Mon Sep 17 00:00:00 2001 From: Simon Wollwage Date: Sat, 6 Dec 2025 08:55:10 +0000 Subject: [PATCH] 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. Signed-off-by: Simon Wollwage PR: 291374 --- lib/libc/stdlib/getopt.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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 */