From: Zbigniew Jędrzejewski-Szmek Date: Fri, 6 May 2022 16:23:06 +0000 (+0200) Subject: basic/strv: fix splitting of strings with escape characters X-Git-Tag: v251-rc3~49^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b38a9d2d77c680fb7ae9b6fb6faf9ab4fe9fc555;p=thirdparty%2Fsystemd.git basic/strv: fix splitting of strings with escape characters Plain strv_split() should not care if the strings contains backslashes or quote characters. But extract_first_word() interprets backslashes unless EXTRACT_RETAIN_ESCAPE is given. I wonder how it's possible that nobody noticed this before. I think this code was introduced in 0645b83a40d1c782f173c4d8440ab2fc82a75006. --- diff --git a/src/basic/strv.h b/src/basic/strv.h index 62950588764..b9563e8692f 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -77,7 +77,7 @@ int strv_split_full(char ***t, const char *s, const char *separators, ExtractFla static inline char** strv_split(const char *s, const char *separators) { char **ret; - if (strv_split_full(&ret, s, separators, 0) < 0) + if (strv_split_full(&ret, s, separators, EXTRACT_RETAIN_ESCAPE) < 0) return NULL; return ret; diff --git a/src/test/test-strv.c b/src/test/test-strv.c index dabb0cd9737..331c277c352 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -317,6 +317,21 @@ TEST(strv_split) { assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 1); assert_se(strv_equal(l, STRV_MAKE("\\"))); + + l = strv_free_erase(l); + + assert_se(l = strv_split("\\", NULL)); + assert_se(strv_equal(l, STRV_MAKE("\\"))); + + l = strv_free_erase(l); + + assert_se(l = strv_split("aa\\ bb\\", NULL)); + assert_se(strv_equal(l, STRV_MAKE("aa\\", "bb\\"))); + + l = strv_free_erase(l); + + assert_se(l = strv_split("aa\" bb'", NULL)); + assert_se(strv_equal(l, STRV_MAKE("aa\"", "bb'"))); } TEST(strv_split_empty) {