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.
This commit is contained in:
Juhani Krekelä 2023-05-16 02:17:22 +03:00
parent 1e17e7fab7
commit 610b51ffdc
1 changed files with 1 additions and 1 deletions

View File

@ -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++];
}