From 610b51ffdc0b89b3c2e5955be01da0c4069a5347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Tue, 16 May 2023 02:17:22 +0300 Subject: [PATCH] Fix getopt_long(3) handling of required argument at the end of argv. Previously if argv ended with a long option that required an argument followed by said argument, getopt_long(3) would generate an error saying that the option requires an argument even though it was provided. This was because the comparison of optind against argc did not account for the fact that optind had already been incremented, causing an off-by-one. --- libc/getopt/getopt_long.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/getopt/getopt_long.c b/libc/getopt/getopt_long.c index 0b2b15be..a6ec2517 100644 --- a/libc/getopt/getopt_long.c +++ b/libc/getopt/getopt_long.c @@ -154,7 +154,7 @@ int getopt_long(int argc, char* const* argv, const char* shortopts, // Check whether the next argument is the parameter to this option. if ( !option_arg && option->has_arg != no_argument ) { - if ( optind + 1 < argc && argv[optind] && + if ( optind < argc && argv[optind] && (option->has_arg == required_argument || argv[optind][0] != '-') ) option_arg = argv[optind++]; }