daandemeyer / rpms / systemd

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