[lldb] Escape semicolons for all shells
authorRaphael Isemann <teemperor@gmail.com>
Wed, 13 Apr 2022 01:12:18 +0000 (18:12 -0700)
committerJonas Devlieghere <jonas@devlieghere.com>
Wed, 13 Apr 2022 01:13:55 +0000 (18:13 -0700)
LLDB supports having globbing regexes in the process launch arguments
that will be resolved using the user's shell. This requires that we pass
the launch args to the shell and then read back the expanded arguments
using LLDB's argdumper utility.

As the shell will not just expand the globbing regexes but all special
characters, we need to escape all non-globbing charcters such as $, &,
<, >, etc. as those otherwise are interpreted and removed in the step
where we expand the globbing characters. Also because the special
characters are shell-specific, LLDB needs to maintain a list of all the
characters that need to be escaped for each specific shell.

This patch adds the missing semicolon character to the escape list for
all currently supported shells. Without this having a semicolon in the
binary path or having a semicolon in the launch arguments will cause the
argdumping process to fail. E.g., lldb -- ./calc "a;b" was failing
before but is working now.

Fixes rdar://55776943

Differential revision: https://reviews.llvm.org/D104629

lldb/source/Utility/Args.cpp
lldb/unittests/Utility/ArgsTest.cpp

index 0334659..8b20ee3 100644 (file)
@@ -384,10 +384,10 @@ std::string Args::GetShellSafeArgument(const FileSpec &shell,
     llvm::StringRef m_escapables;
   };
 
-  static ShellDescriptor g_Shells[] = {{ConstString("bash"), " '\"<>()&"},
-                                       {ConstString("tcsh"), " '\"<>()&$"},
+  static ShellDescriptor g_Shells[] = {{ConstString("bash"), " '\"<>()&;"},
+                                       {ConstString("tcsh"), " '\"<>()&$;"},
                                        {ConstString("zsh"), " '\"<>()&;\\|"},
-                                       {ConstString("sh"), " '\"<>()&"}};
+                                       {ConstString("sh"), " '\"<>()&;"}};
 
   // safe minimal set
   llvm::StringRef escapables = " '\"";
index 0b153a8..dc306b2 100644 (file)
@@ -328,14 +328,25 @@ TEST(ArgsTest, GetShellSafeArgument) {
   // Normal characters and expressions that shouldn't be escaped.
   EXPECT_EQ(Args::GetShellSafeArgument(zsh, "aA$1*"), "aA$1*");
 
-  // String that doesn't need to be escaped
-  EXPECT_EQ(Args::GetShellSafeArgument(bash, "a"), "a");
+  // Test escaping bash special characters.
+  EXPECT_EQ(Args::GetShellSafeArgument(bash, R"( '"<>()&;)"),
+            R"(\ \'\"\<\>\(\)\&\;)");
+  // Normal characters and globbing expressions that shouldn't be escaped.
+  EXPECT_EQ(Args::GetShellSafeArgument(bash, "aA$1*"), "aA$1*");
 
-  // Try escaping with tcsh and the tcsh-specific "$" escape.
+  // Test escaping tcsh special characters.
   FileSpec tcsh("/bin/tcsh", FileSpec::Style::posix);
-  EXPECT_EQ(Args::GetShellSafeArgument(tcsh, "a$b"), "a\\$b");
-  // Bash however doesn't need escaping for "$".
-  EXPECT_EQ(Args::GetShellSafeArgument(bash, "a$b"), "a$b");
+  EXPECT_EQ(Args::GetShellSafeArgument(tcsh, R"( '"<>()&$;)"),
+            R"(\ \'\"\<\>\(\)\&\$\;)");
+  // Normal characters and globbing expressions that shouldn't be escaped.
+  EXPECT_EQ(Args::GetShellSafeArgument(tcsh, "aA1*"), "aA1*");
+
+  // Test escaping sh special characters.
+  FileSpec sh("/bin/sh", FileSpec::Style::posix);
+  EXPECT_EQ(Args::GetShellSafeArgument(sh, R"( '"<>()&;)"),
+            R"(\ \'\"\<\>\(\)\&\;)");
+  // Normal characters and globbing expressions that shouldn't be escaped.
+  EXPECT_EQ(Args::GetShellSafeArgument(sh, "aA$1*"), "aA$1*");
 
   // Try escaping with an unknown shell.
   FileSpec unknown_shell("/bin/unknown_shell", FileSpec::Style::posix);