shared/fstab-util: use free_and_str[n]dup()

No functional change. I'm keeping this separate to make review easier.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-07-31 14:27:14 +02:00
parent ecaf258eb4
commit 0e8d185938
1 changed files with 22 additions and 31 deletions

View 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) {