ryantimwilson / rpms / systemd

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