From 297896fe0ecc8cdb9300d2f14b60ce5c38ba3e32 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 20 Jan 2014 13:13:36 -0600 Subject: [PATCH] getopt(): Could leave optind in undefined state if argc == 1 --- ChangeLog | 2 ++ libc/unistd/lib_getopt.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8673c594f4..51378242fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6485,4 +6485,6 @@ compiler option when building for a 32-bit target on a native 64-bit compiler (2014-1-18). * include/ctype.h: Typo in macro name: iscntrol, not iscontrol (2104-1-17) + * libc/unistd/lib_getopt.c: If there are no arguments (argc == 1), then + getopt() make like the optind variable in an undefined state (2014-1-20). diff --git a/libc/unistd/lib_getopt.c b/libc/unistd/lib_getopt.c index 1a91909765..4399e6140b 100644 --- a/libc/unistd/lib_getopt.c +++ b/libc/unistd/lib_getopt.c @@ -264,6 +264,10 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) return noarg_ret; } + /* Restore the initial, uninitialized state */ + g_binitialized = false; + optind = 1; + optopt = '?'; return ERROR; }