diff options
author | Pavel Labath <pavel@labath.sk> | 2019-06-25 14:02:39 +0000 |
---|---|---|
committer | Pavel Labath <pavel@labath.sk> | 2019-06-25 14:02:39 +0000 |
commit | 34cac0955d7f77a402d2994d6c341fea08d369cf (patch) | |
tree | 033cb2f10edfc4e8275376a9984a0d0760eca3f6 /lldb/source/Interpreter | |
parent | f4e51dd2cd5bcc101533fa84700a0afb7ae911ff (diff) | |
download | bcm5719-llvm-34cac0955d7f77a402d2994d6c341fea08d369cf.tar.gz bcm5719-llvm-34cac0955d7f77a402d2994d6c341fea08d369cf.zip |
Options: Correctly check for missing arguments
Relying on the value of optind for detecting missing arguments is
unreliable because its value after a failed parse is an implementation
detail. A more correct way to achieve this is to pass ':' at the
beginning of option string, which tells getopt to return ':' for missing
arguments.
For this to work, I also had to add a nullptr at the end of the argv
vector, as some getopt implementations did not work without that. This
is also an implementation detail, as getopt should normally be called
with argc+argc "as to main function" (i.e. null-terminated).
Thanks to Michał Górny for testing this patch out on NetBSD.
llvm-svn: 364317
Diffstat (limited to 'lldb/source/Interpreter')
-rw-r--r-- | lldb/source/Interpreter/Options.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/lldb/source/Interpreter/Options.cpp b/lldb/source/Interpreter/Options.cpp index bd9b47fadd4..e015ee4c575 100644 --- a/lldb/source/Interpreter/Options.cpp +++ b/lldb/source/Interpreter/Options.cpp @@ -1336,6 +1336,9 @@ llvm::Expected<Args> Options::Parse(const Args &args, llvm::inconvertibleErrorCode()); } + // Leading : tells getopt to return a : for a missing option argument AND to + // suppress error messages. + sstr << ":"; for (int i = 0; long_options[i].definition != nullptr; ++i) { if (long_options[i].flag == nullptr) { if (isprint8(long_options[i].val)) { @@ -1357,8 +1360,7 @@ llvm::Expected<Args> Options::Parse(const Args &args, std::vector<char *> argv = GetArgvForParsing(args); // If the last option requires an argument but doesn't have one, // some implementations of getopt_long will still try to read it. - char overflow = 0; - argv.push_back(&overflow); + argv.push_back(nullptr); std::unique_lock<std::mutex> lock; OptionParser::Prepare(lock); int val; @@ -1367,7 +1369,7 @@ llvm::Expected<Args> Options::Parse(const Args &args, val = OptionParser::Parse(argv.size() - 1, &*argv.begin(), sstr.GetString(), long_options, &long_options_index); - if ((size_t)OptionParser::GetOptionIndex() > argv.size() - 1) { + if (val == ':') { error.SetErrorStringWithFormat("last option requires an argument"); break; } |