From 996e958ecde418244f6aca999b5b1edaeeeb009b Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Wed, 27 Nov 2013 22:37:52 +0100
Subject: [PATCH] util: fix handling of trailing whitespace in split_quoted()
Inspired by a patch by Lukas Nykryn.
Conflicts:
src/test/test-strv.c
---
src/shared/util.c | 24 +++++++++++++++---------
src/test/test-strv.c | 26 ++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/src/shared/util.c b/src/shared/util.c
index f602625..b1a4006 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -370,17 +370,21 @@ char *split(const char *c, size_t *l, const char *separator, char **state) {
/* Split a string into words, but consider strings enclosed in '' and
* "" as words even if they include spaces. */
char *split_quoted(const char *c, size_t *l, char **state) {
- char *current, *e;
+ const char *current, *e;
bool escaped = false;
- current = *state ? *state : (char*) c;
+ assert(c);
+ assert(l);
+ assert(state);
- if (!*current || *c == 0)
- return NULL;
+ current = *state ? *state : c;
current += strspn(current, WHITESPACE);
- if (*current == '\'') {
+ if (*current == 0)
+ return NULL;
+
+ else if (*current == '\'') {
current ++;
for (e = current; *e; e++) {
@@ -393,7 +397,8 @@ char *split_quoted(const char *c, size_t *l, char **state) {
}
*l = e-current;
- *state = *e == 0 ? e : e+1;
+ *state = (char*) (*e == 0 ? e : e+1);
+
} else if (*current == '\"') {
current ++;
@@ -407,7 +412,8 @@ char *split_quoted(const char *c, size_t *l, char **state) {
}
*l = e-current;
- *state = *e == 0 ? e : e+1;
+ *state = (char*) (*e == 0 ? e : e+1);
+
} else {
for (e = current; *e; e++) {
if (escaped)
@@ -418,10 +424,10 @@ char *split_quoted(const char *c, size_t *l, char **state) {
break;
}
*l = e-current;
- *state = e;
+ *state = (char*) e;
}
- return (char*) current;
+ return current;
}
int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index c3d536d..ed26ad1 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -156,6 +156,20 @@ static void test_strv_quote_unquote(const char* const *split, const char *quoted
}
}
+static void test_strv_quote_unquote2(const char *quoted, const char ** list) {
+ _cleanup_strv_free_ char **s;
+ unsigned i = 0;
+ char **t;
+
+ s = strv_split_quoted(quoted);
+ assert_se(s);
+
+ STRV_FOREACH(t, s)
+ assert_se(streq(list[i++], *t));
+
+ assert_se(list[i] == NULL);
+}
+
static void test_strv_split_nulstr(void) {
_cleanup_strv_free_ char **l = NULL;
const char nulstr[] = "str0\0str1\0str2\0str3\0";
@@ -309,6 +323,18 @@ int main(int argc, char *argv[]) {
test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
+ test_strv_quote_unquote2(" foo=bar \"waldo\" zzz ", (const char*[]) { "foo=bar", "waldo", "zzz", NULL });
+ test_strv_quote_unquote2("", (const char*[]) { NULL });
+ test_strv_quote_unquote2(" ", (const char*[]) { NULL });
+ test_strv_quote_unquote2(" ", (const char*[]) { NULL });
+ test_strv_quote_unquote2(" x", (const char*[]) { "x", NULL });
+ test_strv_quote_unquote2("x ", (const char*[]) { "x", NULL });
+ test_strv_quote_unquote2(" x ", (const char*[]) { "x", NULL });
+ test_strv_quote_unquote2(" \"x\" ", (const char*[]) { "x", NULL });
+ test_strv_quote_unquote2(" \'x\' ", (const char*[]) { "x", NULL });
+ test_strv_quote_unquote2(" \'x\"\' ", (const char*[]) { "x\"", NULL });
+ test_strv_quote_unquote2(" \"x\'\" ", (const char*[]) { "x\'", NULL });
+
test_strv_split_nulstr();
test_strv_parse_nulstr();
test_strv_overlap();