Use ' quoting to escape arguments.
The specific motivating case is "text;ls;1.apk", but rather than continue
adding individual characters to the list of characters to be escaped, let's
just switch to quote all arguments with ', which only leaves ' itself to be
escaped.
Bug: 20323053
Bug: 19734868
Change-Id: I8bd71db9373bc2d1169fc11e46c889da6638550a
diff --git a/adb_utils.cpp b/adb_utils.cpp
index b515f59..34b7f07 100644
--- a/adb_utils.cpp
+++ b/adb_utils.cpp
@@ -33,19 +33,18 @@
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;
}