]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/fstab-util: replace FOREACH_WORD_SEPARATOR() with open-coded loop
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 31 Jul 2020 12:40:23 +0000 (14:40 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 9 Sep 2020 07:34:55 +0000 (09:34 +0200)
The tricky part here is that the function is not allowed to fail in this code
path. Initially, I wanted to change the return value to allow it to fail, but
this cascades through all the places where fstab_test_option() and friends are
used; updating all those sites would be a lot of work. And since quoting is not
allowed here, a simple loop with strcspn() is easy to do.

src/shared/fstab-util.c

index 0e1b61aa954284b80e11c1c3cc42311e88423aca..d883eca5c785d692f06ced398a06b6a743ff9c44 100644 (file)
@@ -94,12 +94,11 @@ int fstab_filter_options(const char *opts, const char *names,
         /* If !ret_value and !ret_filtered, this function is not allowed to fail. */
 
         if (!ret_filtered) {
-                const char *word, *state;
-                size_t l;
+                for (const char *word = opts;;) {
+                        const char *end = word + strcspn(word, ",");
 
-                FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
                         NULSTR_FOREACH(name, names) {
-                                if (l < strlen(name))
+                                if (end < word + strlen(name))
                                         continue;
                                 if (!strneq(word, name, strlen(name)))
                                         continue;
@@ -114,12 +113,20 @@ int fstab_filter_options(const char *opts, const char *names,
 
                                                 r = free_and_strndup(&v,
                                                                      eq ? x + 1 : NULL,
-                                                                     eq ? l - strlen(name) - 1 : 0);
+                                                                     eq ? end - x - 1 : 0);
                                                 if (r < 0)
                                                         return r;
                                         }
+
+                                        break;
                                 }
                         }
+
+                        if (*end)
+                                word = end + 1;
+                        else
+                                break;
+                }
         } else {
                 stor = strv_split(opts, ",");
                 if (!stor)