]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: make sure we eat even half complete words in split()
authorLennart Poettering <lennart@poettering.net>
Thu, 2 Apr 2020 14:36:33 +0000 (16:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 2 Apr 2020 14:43:36 +0000 (16:43 +0200)
split() and FOREACH_WORD really should die, and everything be moved to
extract_first_word() and friends, but let's at least make sure that for
the remaining code using it we can't deadlock by not progressing in the
word iteration.

Fixes: #15305
src/basic/string-util.c
src/test/test-strv.c

index 2eb84babd86fe86acbe566e0ac5b52cbf4f4e756..9983aa826e69cc68dfdfa1fc952bf52c1ab340ab 100644 (file)
@@ -113,7 +113,7 @@ static size_t strcspn_escaped(const char *s, const char *reject) {
         bool escaped = false;
         int n;
 
-        for (n=0; s[n]; n++) {
+        for (n = 0; s[n] != '\0'; n++) {
                 if (escaped)
                         escaped = false;
                 else if (s[n] == '\\')
@@ -122,8 +122,7 @@ static size_t strcspn_escaped(const char *s, const char *reject) {
                         break;
         }
 
-        /* if s ends in \, return index of previous char */
-        return n - escaped;
+        return n;
 }
 
 /* Split a string into words. */
index 68c128cf80d1370f568cbc8a484d39dcc7b5ef0e..5473e983bd843ad11c2880cf614e51837ad331a6 100644 (file)
@@ -307,6 +307,12 @@ static void test_strv_split(void) {
         l = strv_split_full("    'one'  \"  two\t three \"' four  five", NULL, SPLIT_QUOTES | SPLIT_RELAX);
         assert_se(l);
         assert_se(strv_equal(l, (char**) input_table_quoted));
+
+        strv_free_erase(l);
+
+        l = strv_split_full("\\", NULL, SPLIT_QUOTES | SPLIT_RELAX);
+        assert_se(l);
+        assert_se(strv_equal(l, STRV_MAKE("\\")));
 }
 
 static void test_strv_split_empty(void) {