getopt_common(): Correct handling of unsupported long options.
If an unrecognized long option is encountered, we must skip over that argv[] entry or getopt_long() will seriously misbehave. Affects getopt_long() and getopt_long_only() Problem found and fix verified with an updated version of the OS test.
This commit is contained in:
parent
42489759fe
commit
7e1ae24c3c
1 changed files with 21 additions and 2 deletions
|
|
@ -384,7 +384,16 @@ int getopt_common(int argc, FAR char * const argv[],
|
|||
|
||||
/* And parse the long option */
|
||||
|
||||
return getopt_long_option(go, argv, longopts, longindex);
|
||||
ret = getopt_long_option(go, argv, longopts, longindex);
|
||||
if (ret == '?')
|
||||
{
|
||||
/* Skip over the unrecognized long option */
|
||||
|
||||
go->go_optind++;
|
||||
go->go_optptr = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* The -option form is only valid in getop_long_only() mode and
|
||||
|
|
@ -401,8 +410,18 @@ int getopt_common(int argc, FAR char * const argv[],
|
|||
*/
|
||||
|
||||
ret = getopt_long_option(go, argv, longopts, longindex);
|
||||
if (ret != '?' || *(go->go_optptr + 1) != '\0')
|
||||
if (ret != '?')
|
||||
{
|
||||
/* Success or ERROR */
|
||||
|
||||
return ret;
|
||||
}
|
||||
else if (*(go->go_optptr + 1) != '\0')
|
||||
{
|
||||
/* Skip over the unrecognized long option */
|
||||
|
||||
go->go_optind++;
|
||||
go->go_optptr = NULL;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue