diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-11-16 14:54:29 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-11-16 14:54:29 +0000 |
commit | ffd04cf090d2da3f6ea160721bcc2c69e3695a47 (patch) | |
tree | a8d5e1918a70ed3c24700ec5380f41c6944e6fd8 /nuttx/lib | |
parent | 34ee7fc95312d8e5978c568eb129745e51865d43 (diff) | |
download | px4-nuttx-ffd04cf090d2da3f6ea160721bcc2c69e3695a47.tar.gz px4-nuttx-ffd04cf090d2da3f6ea160721bcc2c69e3695a47.tar.bz2 px4-nuttx-ffd04cf090d2da3f6ea160721bcc2c69e3695a47.zip |
Fix a possible sigsegv in getopt
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1252 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib')
-rw-r--r-- | nuttx/lib/lib_getopt.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/nuttx/lib/lib_getopt.c b/nuttx/lib/lib_getopt.c index ab770bb0d..b9b3c2b26 100644 --- a/nuttx/lib/lib_getopt.c +++ b/nuttx/lib/lib_getopt.c @@ -131,7 +131,7 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* Are we resuming in the middle, or at the end of a string of arguments? * g_optptr == NULL means that we are started at the beginning of argv[optind]; - * *g_optptr == means that we are starting at the beginning of optind+1 + * *g_optptr == \0 means that we are starting at the beginning of optind+1 */ while (!g_optptr || !*g_optptr) @@ -147,24 +147,26 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) */ optind++; - if (!argv[optind]) - { - /* There are no more arguments, we are finished */ + } + + /* Check for the end of the argument list */ + + g_optptr = argv[optind]; + if (!g_optptr) + { + /* There are no more arguments, we are finished */ - g_optptr = NULL; - g_binitialized = FALSE; + g_binitialized = FALSE; - /* Return -1 with optind == all of the arguments */ + /* Return -1 with optind == all of the arguments */ - return ERROR; - } + return ERROR; } /* We are starting at the beginning of argv[optind]. In this case, the * first character must be '-' */ - g_optptr = argv[optind]; if (*g_optptr != '-') { /* The argument does not start with '-', we are finished */ @@ -238,9 +240,11 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) } /* No.. is the optional argument the next argument in argv[] ? */ + if (argv[optind+1] && *argv[optind+1] != '-') { - /* Yes.. retun that */ + /* Yes.. return that */ + optarg = argv[optind+1]; optind += 2; g_optptr = NULL; @@ -248,6 +252,7 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) } /* No argument was supplied */ + optarg = NULL; optopt = *optchar; optind++; |