From: Yu Watanabe Date: Thu, 28 Jun 2018 09:18:55 +0000 (+0900) Subject: strv: make strv_split() accept empty string X-Git-Tag: v240~979^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0745ce7565b37e2f7d7150c8c531524f0bd01dd1;p=thirdparty%2Fsystemd.git strv: make strv_split() accept empty string --- diff --git a/src/basic/strv.c b/src/basic/strv.c index b3716233b5a..abf3fc4c7bd 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -253,6 +253,10 @@ char **strv_split(const char *s, const char *separator) { assert(s); + s += strspn(s, separator); + if (isempty(s)) + return new0(char*, 1); + n = 0; FOREACH_WORD_SEPARATOR(word, l, s, separator, state) n++; diff --git a/src/test/test-strv.c b/src/test/test-strv.c index 1c192239a28..340b5628f9b 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -180,12 +180,31 @@ static void test_strv_split(void) { const char str[] = "one,two,three"; l = strv_split(str, ","); - assert_se(l); + STRV_FOREACH(s, l) + assert_se(streq(*s, input_table_multiple[i++])); - STRV_FOREACH(s, l) { + i = 0; + strv_free(l); + + l = strv_split(" one two\t three", WHITESPACE); + assert_se(l); + STRV_FOREACH(s, l) assert_se(streq(*s, input_table_multiple[i++])); - } +} + +static void test_strv_split_empty(void) { + _cleanup_strv_free_ char **l = NULL; + + l = strv_split("", WHITESPACE); + assert_se(l); + assert_se(strv_isempty(l)); + + strv_free(l); + l = strv_split(" ", WHITESPACE); + assert_se(l); + assert_se(strv_isempty(l)); + } static void test_strv_split_extract(void) { @@ -733,6 +752,7 @@ int main(int argc, char *argv[]) { test_invalid_unquote("'x'y'g"); test_strv_split(); + test_strv_split_empty(); test_strv_split_extract(); test_strv_split_newlines(); test_strv_split_nulstr();