summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2018-01-23 22:07:25 -0800
committerWilliam A. Kennington III <wak@google.com>2018-01-25 17:32:11 -0800
commit8728491a0043bb67bdabdf27ffb8db89d930b88a (patch)
tree80cc3981f83b8a13da73b486b8e231fa1ffea0d1
parentbfe5cb8e60cadf19bff81acf926ab078ad438120 (diff)
downloadphosphor-watchdog-8728491a0043bb67bdabdf27ffb8db89d930b88a.tar.gz
phosphor-watchdog-8728491a0043bb67bdabdf27ffb8db89d930b88a.zip
argument: Reset optind before using getopt
The getopt family of functions are globally stateful, we need to make sure that we reset the global state before using it otherwise we can't create arguments more than once. Change-Id: Ic673eb3592217329b95fde033a1f2773b6b980bf Signed-off-by: William A. Kennington III <wak@google.com>
-rw-r--r--argument.cpp7
1 files changed, 7 insertions, 0 deletions
diff --git a/argument.cpp b/argument.cpp
index 41ea9ae..61c97ca 100644
--- a/argument.cpp
+++ b/argument.cpp
@@ -42,6 +42,13 @@ const option ArgumentParser::options[] =
ArgumentParser::ArgumentParser(int argc, char** argv)
{
int option = 0;
+
+ // We have to reset optind because getopt_long keeps global state
+ // and uses optind to track what argv index it needs to process next.
+ // Since this object may be instantiated more than once or test suites may
+ // already process instructions, optind may not be initialized to point to
+ // the beginning of our argv.
+ optind = 0;
while (-1 != (option = getopt_long(argc, argv, optionStr, options, nullptr)))
{
if ((option == '?') || (option == 'h'))
OpenPOWER on IntegriCloud