diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-07-31 14:27:14 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-09-09 09:34:54 +0200 |
commit | 0e8d1859383a4f333d7ffc801b17e33fa19df026 (patch) | |
tree | 3d510c1898335394689d33862ba443b11adefc02 | |
parent | Use extract_first_word() in generated conf parsers (diff) | |
download | systemd-0e8d1859383a4f333d7ffc801b17e33fa19df026.tar.xz systemd-0e8d1859383a4f333d7ffc801b17e33fa19df026.zip |
shared/fstab-util: use free_and_str[n]dup()
No functional change. I'm keeping this separate to make review easier.
-rw-r--r-- | src/shared/fstab-util.c | 53 |
1 files changed, 22 insertions, 31 deletions
diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index 806dda8475..0e1b61aa95 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) { |