Skip to content

Commit

Permalink
Escape boot parameters to quoted space-separated format of Parse_argv
Browse files Browse the repository at this point in the history
Parse_argv.parse is expecting a space separated list of quoted
backslash-escaped strings.  Until now the parameters of argv are just
concatenated with spaces, which results in lost information if argv
elements contain spaces themselves, and the user has to take care of
the quoting and escaping of the boot parameters by themselves.
Together with the escape requirements of typical shells, several
layers of escaping are necessary, which is tedious and error prone.
To avoid this, this change replaces ' ', '"' and '\' with '\ ' '\"' and
'\\' respectively, and joins them with spaces into a single string,
so that Parse_argv.parse can resemble a list of strings identical to
the original argv, which makes additional escaping unnecessary.

Related: Solo5#281
Fixes: mirage/mirage-solo5#33
  • Loading branch information
ansiwen committed Oct 12, 2018
1 parent c994757 commit 04fc8b5
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions tenders/hvt/hvt_main.c
Expand Up @@ -35,23 +35,30 @@

#include "hvt.h"

static void setup_cmdline(char *cmdline, int argc, char **argv)
static void push_cmdline(char c, size_t *pos, char *cmdline)
{
size_t cmdline_free = HVT_CMDLINE_SIZE;

cmdline[0] = 0;
if (*pos > HVT_CMDLINE_SIZE - 1) {
errx(1, "Guest command line too long (max=%d characters)",
HVT_CMDLINE_SIZE - 1);
}
cmdline[(*pos)++] = c;
}

static void setup_cmdline(char *cmdline, int argc, char **argv)
{
size_t pos = 0;
for (; *argv; argc--, argv++) {
size_t alen = snprintf(cmdline, cmdline_free, "%s%s", *argv,
(argc > 1) ? " " : "");
if (alen >= cmdline_free) {
errx(1, "Guest command line too long (max=%d characters)",
HVT_CMDLINE_SIZE - 1);
break;
for (char *p = *argv; *p; p++) {
if (*p == '\\' || *p == '"' || *p == ' ') {
push_cmdline('\\', &pos, cmdline);
}
push_cmdline(*p, &pos, cmdline);
}
if (argc > 1) {
push_cmdline(' ', &pos, cmdline);
}
cmdline_free -= alen;
cmdline += alen;
}
push_cmdline('\0', &pos, cmdline);
}

static void setup_modules(struct hvt *hvt)
Expand Down

0 comments on commit 04fc8b5

Please sign in to comment.