From a6fde35332f5e7f78bff437d7b7bfded83debbaa Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Sat, 31 Aug 2013 20:28:09 +0200 Subject: systemd-run: properly escape arguments Spaces, quotes, and such, were not properly escaped. We should write them like we read them. https://bugs.freedesktop.org/show_bug.cgi?id=67971 --- src/shared/strv.c | 37 +++++++++++++++++++++++++++++++++++++ src/shared/strv.h | 1 + 2 files changed, 38 insertions(+) (limited to 'src/shared') diff --git a/src/shared/strv.c b/src/shared/strv.c index 3e7778d61c..2df478f30b 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -356,6 +356,43 @@ char *strv_join(char **l, const char *separator) { return r; } +char *strv_join_quoted(char **l) { + char *buf = NULL; + char **s; + size_t allocated = 0, len = 0; + + STRV_FOREACH(s, l) { + /* assuming here that escaped string cannot be more + * than twice as long, and reserving space for the + * separator and quotes. + */ + _cleanup_free_ char *esc = NULL; + size_t needed; + + if (!GREEDY_REALLOC(buf, allocated, + len + strlen(*s) * 2 + 3)) + goto oom; + + esc = cescape(*s); + if (!esc) + goto oom; + + needed = snprintf(buf + len, allocated - len, "%s\"%s\"", + len > 0 ? " " : "", esc); + assert(needed < allocated - len); + len += needed; + } + + if (!buf) + buf = malloc0(1); + + return buf; + + oom: + free(buf); + return NULL; +} + char **strv_append(char **l, const char *s) { char **r, **k; diff --git a/src/shared/strv.h b/src/shared/strv.h index 4ade827a42..4e80ea6d89 100644 --- a/src/shared/strv.h +++ b/src/shared/strv.h @@ -68,6 +68,7 @@ char **strv_split_quoted(const char *s); char **strv_split_newlines(const char *s); char *strv_join(char **l, const char *separator); +char *strv_join_quoted(char **l); char **strv_parse_nulstr(const char *s, size_t l); char **strv_split_nulstr(const char *s); -- cgit v1.2.3-54-g00ecf