summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--adb/adb_utils.cpp15
-rw-r--r--adb/adb_utils_test.cpp48
2 files changed, 31 insertions, 32 deletions
diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp
index b515f59..34b7f07 100644
--- a/adb/adb_utils.cpp
+++ b/adb/adb_utils.cpp
@@ -33,19 +33,18 @@ bool directory_exists(const std::string& path) {
return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode);
}
-static bool should_escape(const char c) {
- return (c == ' ' || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')');
-}
-
std::string escape_arg(const std::string& s) {
- // Preserve empty arguments.
- if (s.empty()) return "\"\"";
+ std::string result = s;
- std::string result(s);
+ // Insert a \ before any ' in the string.
for (auto it = result.begin(); it != result.end(); ++it) {
- if (should_escape(*it)) {
+ if (*it == '\'') {
it = result.insert(it, '\\') + 1;
}
}
+
+ // Prefix and suffix the whole string with '.
+ result.insert(result.begin(), '\'');
+ result.push_back('\'');
return result;
}
diff --git a/adb/adb_utils_test.cpp b/adb/adb_utils_test.cpp
index 95e28a8..a395079 100644
--- a/adb/adb_utils_test.cpp
+++ b/adb/adb_utils_test.cpp
@@ -25,28 +25,28 @@ TEST(adb_utils, directory_exists) {
}
TEST(adb_utils, escape_arg) {
- ASSERT_EQ(R"("")", escape_arg(""));
-
- ASSERT_EQ(R"(abc)", escape_arg("abc"));
-
- ASSERT_EQ(R"(\ abc)", escape_arg(" abc"));
- ASSERT_EQ(R"(\'abc)", escape_arg("'abc"));
- ASSERT_EQ(R"(\"abc)", escape_arg("\"abc"));
- ASSERT_EQ(R"(\\abc)", escape_arg("\\abc"));
- ASSERT_EQ(R"(\(abc)", escape_arg("(abc"));
- ASSERT_EQ(R"(\)abc)", escape_arg(")abc"));
-
- ASSERT_EQ(R"(abc\ abc)", escape_arg("abc abc"));
- ASSERT_EQ(R"(abc\'abc)", escape_arg("abc'abc"));
- ASSERT_EQ(R"(abc\"abc)", escape_arg("abc\"abc"));
- ASSERT_EQ(R"(abc\\abc)", escape_arg("abc\\abc"));
- ASSERT_EQ(R"(abc\(abc)", escape_arg("abc(abc"));
- ASSERT_EQ(R"(abc\)abc)", escape_arg("abc)abc"));
-
- ASSERT_EQ(R"(abc\ )", escape_arg("abc "));
- ASSERT_EQ(R"(abc\')", escape_arg("abc'"));
- ASSERT_EQ(R"(abc\")", escape_arg("abc\""));
- ASSERT_EQ(R"(abc\\)", escape_arg("abc\\"));
- ASSERT_EQ(R"(abc\()", escape_arg("abc("));
- ASSERT_EQ(R"(abc\))", escape_arg("abc)"));
+ ASSERT_EQ(R"('')", escape_arg(""));
+
+ ASSERT_EQ(R"('abc')", escape_arg("abc"));
+
+ ASSERT_EQ(R"(' abc')", escape_arg(" abc"));
+ ASSERT_EQ(R"('\'abc')", escape_arg("'abc"));
+ ASSERT_EQ(R"('"abc')", escape_arg("\"abc"));
+ ASSERT_EQ(R"('\abc')", escape_arg("\\abc"));
+ ASSERT_EQ(R"('(abc')", escape_arg("(abc"));
+ ASSERT_EQ(R"(')abc')", escape_arg(")abc"));
+
+ ASSERT_EQ(R"('abc abc')", escape_arg("abc abc"));
+ ASSERT_EQ(R"('abc\'abc')", escape_arg("abc'abc"));
+ ASSERT_EQ(R"('abc"abc')", escape_arg("abc\"abc"));
+ ASSERT_EQ(R"('abc\abc')", escape_arg("abc\\abc"));
+ ASSERT_EQ(R"('abc(abc')", escape_arg("abc(abc"));
+ ASSERT_EQ(R"('abc)abc')", escape_arg("abc)abc"));
+
+ ASSERT_EQ(R"('abc ')", escape_arg("abc "));
+ ASSERT_EQ(R"('abc\'')", escape_arg("abc'"));
+ ASSERT_EQ(R"('abc"')", escape_arg("abc\""));
+ ASSERT_EQ(R"('abc\')", escape_arg("abc\\"));
+ ASSERT_EQ(R"('abc(')", escape_arg("abc("));
+ ASSERT_EQ(R"('abc)')", escape_arg("abc)"));
}