[Lldb-commits] [lldb] r364260 - Reapply "Fix a crash in option parsing."

Adrian Prantl via lldb-commits lldb-commits at lists.llvm.org
Mon Jun 24 17:55:28 PDT 2019


Author: adrian
Date: Mon Jun 24 17:55:27 2019
New Revision: 364260

URL: http://llvm.org/viewvc/llvm-project?rev=364260&view=rev
Log:
Reapply "Fix a crash in option parsing."

with an additional read-out-of-bounds bugfix applied.

Differential Revision: https://reviews.llvm.org/D63110

Added:
    lldb/trunk/lit/Driver/Inputs/process_attach_pid.in
    lldb/trunk/lit/Driver/TestProcessAttach.test
Modified:
    lldb/trunk/source/Interpreter/Options.cpp

Added: lldb/trunk/lit/Driver/Inputs/process_attach_pid.in
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Driver/Inputs/process_attach_pid.in?rev=364260&view=auto
==============================================================================
--- lldb/trunk/lit/Driver/Inputs/process_attach_pid.in (added)
+++ lldb/trunk/lit/Driver/Inputs/process_attach_pid.in Mon Jun 24 17:55:27 2019
@@ -0,0 +1,2 @@
+process attach --pid
+

Added: lldb/trunk/lit/Driver/TestProcessAttach.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Driver/TestProcessAttach.test?rev=364260&view=auto
==============================================================================
--- lldb/trunk/lit/Driver/TestProcessAttach.test (added)
+++ lldb/trunk/lit/Driver/TestProcessAttach.test Mon Jun 24 17:55:27 2019
@@ -0,0 +1,2 @@
+# RUN: %lldb -x -b -S %S/Inputs/process_attach_pid.in 2>&1 | FileCheck %s
+# CHECK: last option requires an argument

Modified: lldb/trunk/source/Interpreter/Options.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/Options.cpp?rev=364260&r1=364259&r2=364260&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/Options.cpp (original)
+++ lldb/trunk/source/Interpreter/Options.cpp Mon Jun 24 17:55:27 2019
@@ -1355,13 +1355,23 @@ llvm::Expected<Args> Options::Parse(cons
     }
   }
   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);
   std::unique_lock<std::mutex> lock;
   OptionParser::Prepare(lock);
   int val;
   while (true) {
     int long_options_index = -1;
-    val = OptionParser::Parse(argv.size(), &*argv.begin(), sstr.GetString(),
+    val = OptionParser::Parse(argv.size() - 1, &*argv.begin(), sstr.GetString(),
                               long_options, &long_options_index);
+
+    if ((size_t)OptionParser::GetOptionIndex() > argv.size() - 1) {
+      error.SetErrorStringWithFormat("last option requires an argument");
+      break;
+    }
+
     if (val == -1)
       break;
 
@@ -1439,6 +1449,7 @@ llvm::Expected<Args> Options::Parse(cons
   if (error.Fail())
     return error.ToError();
 
+  argv.pop_back();
   argv.erase(argv.begin(), argv.begin() + OptionParser::GetOptionIndex());
   return ReconstituteArgsAfterParsing(argv, args);
 }




More information about the lldb-commits mailing list