Merge changes I2998e40a,I03eacfa1

* changes:
  Partially revert argument escaping.
  Uniformly escape shell arguments.
This commit is contained in:
Elliott Hughes 2014-08-05 22:55:56 +00:00 committed by Gerrit Code Review
commit 93266ed1f7

View file

@ -544,39 +544,40 @@ static void status_window(transport_type ttype, const char* serial)
} }
} }
/** duplicate string and quote all \ " ( ) chars + space character. */ /** Duplicate and escape given argument. */
static char * static char *escape_arg(const char *s)
dupAndQuote(const char *s)
{ {
const char *ts; const char *ts;
size_t alloc_len; size_t alloc_len;
char *ret; char *ret;
char *dest; char *dest;
ts = s;
alloc_len = 0; alloc_len = 0;
for (ts = s; *ts != '\0'; ts++) {
for( ;*ts != '\0'; ts++) {
alloc_len++; alloc_len++;
if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') { if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
alloc_len++; alloc_len++;
} }
} }
ret = (char *)malloc(alloc_len + 1); if (alloc_len == 0) {
// Preserve empty arguments
ret = (char *) malloc(3);
ret[0] = '\"';
ret[1] = '\"';
ret[2] = '\0';
return ret;
}
ts = s; ret = (char *) malloc(alloc_len + 1);
dest = ret; dest = ret;
for ( ;*ts != '\0'; ts++) { for (ts = s; *ts != '\0'; ts++) {
if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') { if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
*dest++ = '\\'; *dest++ = '\\';
} }
*dest++ = *ts; *dest++ = *ts;
} }
*dest++ = '\0'; *dest++ = '\0';
return ret; return ret;
@ -683,30 +684,24 @@ static int logcat(transport_type transport, char* serial, int argc, char **argv)
char buf[4096]; char buf[4096];
char *log_tags; char *log_tags;
char *quoted_log_tags; char *quoted;
log_tags = getenv("ANDROID_LOG_TAGS"); log_tags = getenv("ANDROID_LOG_TAGS");
quoted_log_tags = dupAndQuote(log_tags == NULL ? "" : log_tags); quoted = escape_arg(log_tags == NULL ? "" : log_tags);
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"shell:export ANDROID_LOG_TAGS=\"\%s\" ; exec logcat", "shell:export ANDROID_LOG_TAGS=\"%s\"; exec logcat", quoted);
quoted_log_tags); free(quoted);
free(quoted_log_tags); if (!strcmp(argv[0], "longcat")) {
strncat(buf, " -v long", sizeof(buf) - 1);
if (!strcmp(argv[0],"longcat")) {
strncat(buf, " -v long", sizeof(buf)-1);
} }
argc -= 1; argc -= 1;
argv += 1; argv += 1;
while(argc-- > 0) { while(argc-- > 0) {
char *quoted; quoted = escape_arg(*argv++);
strncat(buf, " ", sizeof(buf) - 1);
quoted = dupAndQuote (*argv++); strncat(buf, quoted, sizeof(buf) - 1);
strncat(buf, " ", sizeof(buf)-1);
strncat(buf, quoted, sizeof(buf)-1);
free(quoted); free(quoted);
} }
@ -1218,7 +1213,7 @@ top:
argc -= 2; argc -= 2;
argv += 2; argv += 2;
while (argc-- > 0) { while (argc-- > 0) {
char *quoted = dupAndQuote(*argv++); char *quoted = escape_arg(*argv++);
strncat(buf, " ", sizeof(buf) - 1); strncat(buf, " ", sizeof(buf) - 1);
strncat(buf, quoted, sizeof(buf) - 1); strncat(buf, quoted, sizeof(buf) - 1);
free(quoted); free(quoted);
@ -1261,7 +1256,7 @@ top:
argc -= 2; argc -= 2;
argv += 2; argv += 2;
while (argc-- > 0) { while (argc-- > 0) {
char *quoted = dupAndQuote(*argv++); char *quoted = escape_arg(*argv++);
strncat(buf, " ", sizeof(buf) - 1); strncat(buf, " ", sizeof(buf) - 1);
strncat(buf, quoted, sizeof(buf) - 1); strncat(buf, quoted, sizeof(buf) - 1);
free(quoted); free(quoted);
@ -1686,12 +1681,9 @@ static int pm_command(transport_type transport, char* serial,
snprintf(buf, sizeof(buf), "shell:pm"); snprintf(buf, sizeof(buf), "shell:pm");
while(argc-- > 0) { while(argc-- > 0) {
char *quoted; char *quoted = escape_arg(*argv++);
strncat(buf, " ", sizeof(buf) - 1);
quoted = dupAndQuote(*argv++); strncat(buf, quoted, sizeof(buf) - 1);
strncat(buf, " ", sizeof(buf)-1);
strncat(buf, quoted, sizeof(buf)-1);
free(quoted); free(quoted);
} }
@ -1723,7 +1715,7 @@ static int delete_file(transport_type transport, char* serial, char* filename)
char* quoted; char* quoted;
snprintf(buf, sizeof(buf), "shell:rm "); snprintf(buf, sizeof(buf), "shell:rm ");
quoted = dupAndQuote(filename); quoted = escape_arg(filename);
strncat(buf, quoted, sizeof(buf)-1); strncat(buf, quoted, sizeof(buf)-1);
free(quoted); free(quoted);