naccyde / rpms / systemd

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