mirror of
https://github.com/apache/nuttx.git
synced 2026-06-04 14:53:47 +08:00
Fix Use of Variable before NULL check
Found a place in getopt_common() where the option string is used before it is checked if it is NULL. This can happen because the short option string is optional for getopt_long() and getopt_long_only() If optstring is NULL, that would be an ERROR for getopt(), but not for the getopt_long() versions. Should effect only the getopt() APIs Tested on the simulator using apps/testing/ostest.
This commit is contained in:
committed by
Abdelatif Guettouche
parent
f878390c6d
commit
6c507730bd
@@ -281,16 +281,6 @@ int getopt_common(int argc, FAR char * const argv[],
|
|||||||
go->go_binitialized = true; /* Now we are initialized */
|
go->go_binitialized = true; /* Now we are initialized */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the first character of opstring s ':', then ':' is in the event
|
|
||||||
* of a missing argument. Otherwise '?' is returned.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (*optstring == ':')
|
|
||||||
{
|
|
||||||
noarg_ret = ':';
|
|
||||||
optstring++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Are we resuming in the middle, or at the end of a string of
|
/* Are we resuming in the middle, or at the end of a string of
|
||||||
* arguments? optptr == NULL means that we are started at the
|
* arguments? optptr == NULL means that we are started at the
|
||||||
* beginning of argv[optind]; *optptr == \0 means that we are
|
* beginning of argv[optind]; *optptr == \0 means that we are
|
||||||
@@ -426,9 +416,43 @@ int getopt_common(int argc, FAR char * const argv[],
|
|||||||
|
|
||||||
if (optstring == NULL)
|
if (optstring == NULL)
|
||||||
{
|
{
|
||||||
goto errout;
|
/* Not an error with getopt_long() */
|
||||||
|
|
||||||
|
if (GETOPT_HAVE_LONG(mode))
|
||||||
|
{
|
||||||
|
/* Return '?'. optptr is reset to the next argv entry,
|
||||||
|
* discarding everything else that follows in the argv string
|
||||||
|
* (which could be another single character command).
|
||||||
|
*/
|
||||||
|
|
||||||
|
go->go_optopt = *go->go_optptr;
|
||||||
|
go->go_optptr = NULL;
|
||||||
|
go->go_optind++;
|
||||||
|
return '?';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Restore the initial, uninitialized state, and return an
|
||||||
|
* error.
|
||||||
|
*/
|
||||||
|
|
||||||
|
go->go_binitialized = false;
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If the first character of opstring s ':', then ':' is in the event
|
||||||
|
* of a missing argument. Otherwise '?' is returned.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (*optstring == ':')
|
||||||
|
{
|
||||||
|
noarg_ret = ':';
|
||||||
|
optstring++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the option appears in 'optstring' */
|
||||||
|
|
||||||
optchar = strchr(optstring, *go->go_optptr);
|
optchar = strchr(optstring, *go->go_optptr);
|
||||||
if (!optchar)
|
if (!optchar)
|
||||||
{
|
{
|
||||||
@@ -439,7 +463,7 @@ int getopt_common(int argc, FAR char * const argv[],
|
|||||||
return '?';
|
return '?';
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Yes, the character is in the list of valid options. Does it have an
|
/* Yes, the character is in the list of valid options. Does it have a
|
||||||
* required argument?
|
* required argument?
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -489,9 +513,7 @@ int getopt_common(int argc, FAR char * const argv[],
|
|||||||
return (optchar[2] == ':') ? *optchar : noarg_ret;
|
return (optchar[2] == ':') ? *optchar : noarg_ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
errout:
|
/* Restore the initial, uninitialized state, and return an error. */
|
||||||
|
|
||||||
/* Restore the initial, uninitialized state */
|
|
||||||
|
|
||||||
go->go_binitialized = false;
|
go->go_binitialized = false;
|
||||||
return ERROR;
|
return ERROR;
|
||||||
|
|||||||
Reference in New Issue
Block a user