From: Zbigniew Jędrzejewski-Szmek Date: Fri, 31 Jul 2020 12:27:14 +0000 (+0200) Subject: shared/fstab-util: use free_and_str[n]dup() X-Git-Tag: v247-rc1~275^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0e8d1859383a4f333d7ffc801b17e33fa19df026;p=thirdparty%2Fsystemd.git shared/fstab-util: use free_and_str[n]dup() No functional change. I'm keeping this separate to make review easier. --- diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index 806dda84754..0e1b61aa954 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -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) {