diff options
author | Elliott Hughes <enh@google.com> | 2015-04-21 23:03:16 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-04-21 23:03:16 +0000 |
commit | 4184a72c4e849931066fa60a2336af1de6dbcd05 (patch) | |
tree | ed6bbb9f5c475f5ddc8c229b1b407732bdafdd6b | |
parent | de165446ede9f663b24a2aeec91c34a6758db1ad (diff) | |
parent | a9caadf8bd291d3e2479d46960bfe225f9914a8b (diff) | |
download | system_core-4184a72c4e849931066fa60a2336af1de6dbcd05.zip system_core-4184a72c4e849931066fa60a2336af1de6dbcd05.tar.gz system_core-4184a72c4e849931066fa60a2336af1de6dbcd05.tar.bz2 |
am a9caadf8: am 14b65736: Merge "Use \' quoting to escape arguments."
* commit 'a9caadf8bd291d3e2479d46960bfe225f9914a8b':
Use ' quoting to escape arguments.
-rw-r--r-- | adb/adb_utils.cpp | 15 | ||||
-rw-r--r-- | adb/adb_utils_test.cpp | 48 |
2 files changed, 31 insertions, 32 deletions
diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp index 710ef3c..f10c143 100644 --- a/adb/adb_utils.cpp +++ b/adb/adb_utils.cpp @@ -35,19 +35,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)")); } |