This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix NULL pointer access in getopt_internal()


The getopt() family functions assume a NULL terminated argument vector
(argv).  In case the ordering is PERMUTE, then the option processing
stops if argv[data->optind] == NULL.  Do this also for the alternative
orderings to avoid later NULL pointer read access.

newlib/ChangeLog
2013-11-12  Sebastian Huber  <sebastian.huber@embedded-brains.de>

	* libc/libc/stdlib/getopt.c (getopt_internal): Fix NULL pointer
	access.
---
 newlib/libc/stdlib/getopt.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/newlib/libc/stdlib/getopt.c b/newlib/libc/stdlib/getopt.c
index 2bea694..bb3a070 100644
--- a/newlib/libc/stdlib/getopt.c
+++ b/newlib/libc/stdlib/getopt.c
@@ -262,9 +262,13 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 	      data->optarg = argv[data->optind++];
 	      return (data->optopt = 1);
 	    }
+	  else if (argv[data->optind] == 0)
+	    {
+	      return EOF;
+	    }
 	  break;
 	case REQUIRE_ORDER:
-	  if (!is_option (argv[data->optind], only))
+	  if (argv[data->optind] == 0 || !is_option (argv[data->optind], only))
 	    return EOF;
 	  break;
 	}
-- 
1.7.7


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]