[Lldb-commits] [lldb] 07a722c - [lldb] Escape semicolons for all shells

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 12 18:14:01 PDT 2022


Author: Raphael Isemann
Date: 2022-04-12T18:13:55-07:00
New Revision: 07a722c574d1260ea63f1c7b7eb235df19af8f69

URL: https://github.com/llvm/llvm-project/commit/07a722c574d1260ea63f1c7b7eb235df19af8f69
DIFF: https://github.com/llvm/llvm-project/commit/07a722c574d1260ea63f1c7b7eb235df19af8f69.diff

LOG: [lldb] Escape semicolons for all shells

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lldb/source/Utility/Args.cpp b/lldb/source/Utility/Args.cpp
index 0334659ab7dd7..8b20ee3f05ee9 100644
--- a/lldb/source/Utility/Args.cpp
+++ b/lldb/source/Utility/Args.cpp
@@ -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 = " '\"";

diff  --git a/lldb/unittests/Utility/ArgsTest.cpp b/lldb/unittests/Utility/ArgsTest.cpp
index 0b153a8d5936a..dc306b209a24d 100644
--- a/lldb/unittests/Utility/ArgsTest.cpp
+++ b/lldb/unittests/Utility/ArgsTest.cpp
@@ -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);


        


More information about the lldb-commits mailing list