]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/fstab-util: use free_and_str[n]dup()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 31 Jul 2020 12:27:14 +0000 (14:27 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 9 Sep 2020 07:34:54 +0000 (09:34 +0200)
No functional change. I'm keeping this separate to make review easier.

src/shared/fstab-util.c

index 806dda84754cab596bb9caf858b1c0ca0b537ade..0e1b61aa954284b80e11c1c3cc42311e88423aca 100644 (file)
@@ -81,16 +81,17 @@ int fstab_is_mount_point(const char *mount) {
 
 int fstab_filter_options(const char *opts, const char *names,
                          const char **ret_namefound, char **ret_value, char **ret_filtered) {
-        const char *name, *n = NULL, *x;
+        const char *name, *namefound = NULL, *x;
         _cleanup_strv_free_ char **stor = NULL;
         _cleanup_free_ char *v = NULL, **strv = NULL;
+        int r;
 
         assert(names && *names);
 
         if (!opts)
                 goto answer;
 
-        /* If !value and !filtered, this function is not allowed to fail. */
+        /* If !ret_value and !ret_filtered, this function is not allowed to fail. */
 
         if (!ret_filtered) {
                 const char *word, *state;
@@ -103,28 +104,23 @@ int fstab_filter_options(const char *opts, const char *names,
                                 if (!strneq(word, name, strlen(name)))
                                         continue;
 
-                                /* we know that the string is NUL
-                                 * terminated, so *x is valid */
+                                /* We know that the string is NUL terminated, so *x is valid */
                                 x = word + strlen(name);
                                 if (IN_SET(*x, '\0', '=', ',')) {
-                                        n = name;
+                                        namefound = name;
                                         if (ret_value) {
-                                                free(v);
-                                                if (IN_SET(*x, '\0', ','))
-                                                        v = NULL;
-                                                else {
-                                                        assert(*x == '=');
-                                                        x++;
-                                                        v = strndup(x, l - strlen(name) - 1);
-                                                        if (!v)
-                                                                return -ENOMEM;
-                                                }
+                                                bool eq = *x == '=';
+                                                assert(eq || IN_SET(*x, ',', '\0'));
+
+                                                r = free_and_strndup(&v,
+                                                                     eq ? x + 1 : NULL,
+                                                                     eq ? l - strlen(name) - 1 : 0);
+                                                if (r < 0)
+                                                        return r;
                                         }
                                 }
                         }
         } else {
-                char **t, **s;
-
                 stor = strv_split(opts, ",");
                 if (!stor)
                         return -ENOMEM;
@@ -132,7 +128,8 @@ int fstab_filter_options(const char *opts, const char *names,
                 if (!strv)
                         return -ENOMEM;
 
-                for (s = t = strv; *s; s++) {
+                char **t = strv;
+                for (char **s = strv; *s; s++) {
                         NULSTR_FOREACH(name, names) {
                                 x = startswith(*s, name);
                                 if (x && IN_SET(*x, '\0', '='))
@@ -144,18 +141,12 @@ int fstab_filter_options(const char *opts, const char *names,
                         continue;
                 found:
                         /* Keep the last occurrence found */
-                        n = name;
+                        namefound = name;
                         if (ret_value) {
-                                free(v);
-                                if (*x == '\0')
-                                        v = NULL;
-                                else {
-                                        assert(*x == '=');
-                                        x++;
-                                        v = strdup(x);
-                                        if (!v)
-                                                return -ENOMEM;
-                                }
+                                assert(IN_SET(*x, '=', '\0'));
+                                r = free_and_strdup(&v, *x == '=' ? x + 1 : NULL);
+                                if (r < 0)
+                                        return r;
                         }
                 }
                 *t = NULL;
@@ -163,7 +154,7 @@ int fstab_filter_options(const char *opts, const char *names,
 
 answer:
         if (ret_namefound)
-                *ret_namefound = n;
+                *ret_namefound = namefound;
         if (ret_filtered) {
                 char *f;
 
@@ -176,7 +167,7 @@ answer:
         if (ret_value)
                 *ret_value = TAKE_PTR(v);
 
-        return !!n;
+        return !!namefound;
 }
 
 int fstab_extract_values(const char *opts, const char *name, char ***values) {