[Lldb-commits] [lldb] r231394 - Correctly quote arguments in LLDB driver

Pavel Labath labath at google.com
Thu Mar 5 11:17:57 PST 2015


Author: labath
Date: Thu Mar  5 13:17:56 2015
New Revision: 231394

URL: http://llvm.org/viewvc/llvm-project?rev=231394&view=rev
Log:
Correctly quote arguments in LLDB driver

Summary:
LLDB driver was simply tacking quotes around the strings in lldb commands, hoping that will work.
This changes it to properly escape quotes and backslashes.

Reviewers: clayborg

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D8083

Modified:
    lldb/trunk/tools/driver/Driver.cpp

Modified: lldb/trunk/tools/driver/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/driver/Driver.cpp?rev=231394&r1=231393&r2=231394&view=diff
==============================================================================
--- lldb/trunk/tools/driver/Driver.cpp (original)
+++ lldb/trunk/tools/driver/Driver.cpp Thu Mar  5 13:17:56 2015
@@ -956,6 +956,18 @@ CleanupAfterCommandSourcing (int fds[2])
 
 }
 
+std::string
+EscapeString (std::string arg)
+{
+    std::string::size_type pos = 0;
+    while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos)
+    {
+        arg.insert (pos, 1, '\\');
+        pos += 2;
+    }
+    return '"' + arg + '"';
+}
+
 void
 Driver::MainLoop ()
 {
@@ -1005,13 +1017,13 @@ Driver::MainLoop ()
     {
         char arch_name[64];
         if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
-            commands_stream.Printf("target create --arch=%s \"%s\"", arch_name, m_option_data.m_args[0].c_str());
+            commands_stream.Printf("target create --arch=%s %s", arch_name, EscapeString(m_option_data.m_args[0]).c_str());
         else
-            commands_stream.Printf("target create \"%s\"", m_option_data.m_args[0].c_str());
+            commands_stream.Printf("target create %s", EscapeString(m_option_data.m_args[0]).c_str());
 
         if (!m_option_data.m_core_file.empty())
         {
-            commands_stream.Printf(" --core \"%s\"", m_option_data.m_core_file.c_str());
+            commands_stream.Printf(" --core %s", EscapeString(m_option_data.m_core_file).c_str());
         }
         commands_stream.Printf("\n");
         
@@ -1019,23 +1031,17 @@ Driver::MainLoop ()
         {
             commands_stream.Printf ("settings set -- target.run-args ");
             for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
-            {
-                const char *arg_cstr = m_option_data.m_args[arg_idx].c_str();
-                if (strchr(arg_cstr, '"') == NULL)
-                    commands_stream.Printf(" \"%s\"", arg_cstr);
-                else
-                    commands_stream.Printf(" '%s'", arg_cstr);
-            }
+                commands_stream.Printf(" %s", EscapeString(m_option_data.m_args[arg_idx]).c_str());
             commands_stream.Printf("\n");
         }
     }
     else if (!m_option_data.m_core_file.empty())
     {
-        commands_stream.Printf("target create --core \"%s\"\n", m_option_data.m_core_file.c_str());
+        commands_stream.Printf("target create --core %s\n", EscapeString(m_option_data.m_core_file).c_str());
     }
     else if (!m_option_data.m_process_name.empty())
     {
-        commands_stream.Printf ("process attach --name \"%s\"", m_option_data.m_process_name.c_str());
+        commands_stream.Printf ("process attach --name %s", EscapeString(m_option_data.m_process_name).c_str());
         
         if (m_option_data.m_wait_for)
             commands_stream.Printf(" --waitfor");





More information about the lldb-commits mailing list