JDevlieghere created this revision.
JDevlieghere added a reviewer: aprantl.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
JDevlieghere requested review of this revision.

Fix escaping when launching in terminal with AppleScript. The invocation we're 
building up is wrapped in single quotes when passed to bash and wrapped in 
double quotes for AppleScript.

Here's an example invocation with the new escaping:

  tell application "Terminal"
     activate
          do script "/bin/bash -c 'arch -arch arm64 
'/Users/jonas/llvm/build-ra/bin/darwin-debug' --unix-socket=/tmp/dL2jSh 
--arch=arm64 --working-dir \"/private/tmp/with spaces\" --disable-aslr 
--env=\"OS_ACTIVITY_DT_MODE=enable\" --  
\"/Users/jonas/llvm/build-ra/bin/lldb-instr\" \"foo bar\" \"baz quux\" ; echo 
Process exited with status $?';exit"
  end tell

Previously we were using unescaped single quotes which resulted in the whole 
bash invocation being passed in pieces. That works most of the time but breaks 
when you have a space in your current working directory for example.


https://reviews.llvm.org/D124568

Files:
  lldb/source/Host/macosx/objcxx/Host.mm


Index: lldb/source/Host/macosx/objcxx/Host.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/Host.mm
+++ lldb/source/Host/macosx/objcxx/Host.mm
@@ -219,11 +219,11 @@
 
   FileSpec working_dir{launch_info.GetWorkingDirectory()};
   if (working_dir)
-    command.Printf(" --working-dir '%s'", working_dir.GetCString());
+    command.Printf(" --working-dir \\\"%s\\\"", working_dir.GetCString());
   else {
     char cwd[PATH_MAX];
     if (getcwd(cwd, PATH_MAX))
-      command.Printf(" --working-dir '%s'", cwd);
+      command.Printf(" --working-dir \\\"%s\\\"", cwd);
   }
 
   if (launch_info.GetFlags().Test(eLaunchFlagDisableASLR))
@@ -239,7 +239,7 @@
   for (const auto &KV : launch_info.GetEnvironment()) {
     auto host_entry = host_env.find(KV.first());
     if (host_entry == host_env.end() || host_entry->second != KV.second)
-      command.Format(" --env='{0}'", Environment::compose(KV));
+      command.Format(" --env=\\\"{0}\\\"", Environment::compose(KV));
   }
 
   command.PutCString(" -- ");
@@ -248,12 +248,12 @@
   if (argv) {
     for (size_t i = 0; argv[i] != NULL; ++i) {
       if (i == 0)
-        command.Printf(" '%s'", exe_path);
+        command.Printf(" \\\"%s\\\"", exe_path);
       else
-        command.Printf(" '%s'", argv[i]);
+        command.Printf(" \\\"%s\\\"", argv[i]);
     }
   } else {
-    command.Printf(" '%s'", exe_path);
+    command.Printf(" \\\"%s\\\"", exe_path);
   }
   command.PutCString(" ; echo Process exited with status $?");
   if (launch_info.GetFlags().Test(lldb::eLaunchFlagCloseTTYOnExit))
@@ -263,6 +263,9 @@
 
   applescript_source.Printf(applscript_in_new_tty,
                             command.GetString().str().c_str());
+
+  printf("%s\n", applescript_source.GetString().str().c_str());
+
   NSAppleScript *applescript = [[NSAppleScript alloc]
       initWithSource:[NSString stringWithCString:applescript_source.GetString()
                                                      .str()


Index: lldb/source/Host/macosx/objcxx/Host.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/Host.mm
+++ lldb/source/Host/macosx/objcxx/Host.mm
@@ -219,11 +219,11 @@
 
   FileSpec working_dir{launch_info.GetWorkingDirectory()};
   if (working_dir)
-    command.Printf(" --working-dir '%s'", working_dir.GetCString());
+    command.Printf(" --working-dir \\\"%s\\\"", working_dir.GetCString());
   else {
     char cwd[PATH_MAX];
     if (getcwd(cwd, PATH_MAX))
-      command.Printf(" --working-dir '%s'", cwd);
+      command.Printf(" --working-dir \\\"%s\\\"", cwd);
   }
 
   if (launch_info.GetFlags().Test(eLaunchFlagDisableASLR))
@@ -239,7 +239,7 @@
   for (const auto &KV : launch_info.GetEnvironment()) {
     auto host_entry = host_env.find(KV.first());
     if (host_entry == host_env.end() || host_entry->second != KV.second)
-      command.Format(" --env='{0}'", Environment::compose(KV));
+      command.Format(" --env=\\\"{0}\\\"", Environment::compose(KV));
   }
 
   command.PutCString(" -- ");
@@ -248,12 +248,12 @@
   if (argv) {
     for (size_t i = 0; argv[i] != NULL; ++i) {
       if (i == 0)
-        command.Printf(" '%s'", exe_path);
+        command.Printf(" \\\"%s\\\"", exe_path);
       else
-        command.Printf(" '%s'", argv[i]);
+        command.Printf(" \\\"%s\\\"", argv[i]);
     }
   } else {
-    command.Printf(" '%s'", exe_path);
+    command.Printf(" \\\"%s\\\"", exe_path);
   }
   command.PutCString(" ; echo Process exited with status $?");
   if (launch_info.GetFlags().Test(lldb::eLaunchFlagCloseTTYOnExit))
@@ -263,6 +263,9 @@
 
   applescript_source.Printf(applscript_in_new_tty,
                             command.GetString().str().c_str());
+
+  printf("%s\n", applescript_source.GetString().str().c_str());
+
   NSAppleScript *applescript = [[NSAppleScript alloc]
       initWithSource:[NSString stringWithCString:applescript_source.GetString()
                                                      .str()
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to