Skip to content

Commit

Permalink
libzutil: zfs_resolve_shortname: don't strdup() ZPOOL_IMPORT_PATH
Browse files Browse the repository at this point in the history
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Ahelenia Ziemiańska <[email protected]>
Closes openzfs#13223
  • Loading branch information
nabijaczleweli authored and behlendorf committed Mar 23, 2022
1 parent 0189378 commit 8a2ed86
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lib/libzutil/zutil_device_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,36 @@ zfs_dirnamelen(const char *path)
int
zfs_resolve_shortname(const char *name, char *path, size_t len)
{
int i, error = -1;
char *dir, *env, *envdup, *tmp = NULL;

env = getenv("ZPOOL_IMPORT_PATH");
errno = ENOENT;
const char *env = getenv("ZPOOL_IMPORT_PATH");

if (env) {
envdup = strdup(env);
for (dir = strtok_r(envdup, ":", &tmp);
dir != NULL && error != 0;
dir = strtok_r(NULL, ":", &tmp)) {
(void) snprintf(path, len, "%s/%s", dir, name);
error = access(path, F_OK);
for (;;) {
env += strspn(env, ":");
size_t dirlen = strcspn(env, ":");
if (dirlen) {
(void) snprintf(path, len, "%.*s/%s",
(int)dirlen, env, name);
if (access(path, F_OK) == 0)
return (0);

env += dirlen;
} else
break;
}
free(envdup);
} else {
const char * const *zpool_default_import_path;
size_t count;
const char *const *zpool_default_import_path =
zpool_default_search_paths(&count);

zpool_default_import_path = zpool_default_search_paths(&count);

for (i = 0; i < count && error < 0; i++) {
for (size_t i = 0; i < count; ++i) {
(void) snprintf(path, len, "%s/%s",
zpool_default_import_path[i], name);
error = access(path, F_OK);
if (access(path, F_OK) == 0)
return (0);
}
}

return (error ? ENOENT : 0);
return (errno = ENOENT);
}

/*
Expand All @@ -100,7 +101,7 @@ zfs_strcmp_shortname(const char *name, const char *cmp_name, int wholedisk)
int path_len, cmp_len, i = 0, error = ENOENT;
char *dir, *env, *envdup = NULL, *tmp = NULL;
char path_name[MAXPATHLEN];
const char * const *zpool_default_import_path = NULL;
const char *const *zpool_default_import_path = NULL;
size_t count;

cmp_len = strlen(cmp_name);
Expand Down

0 comments on commit 8a2ed86

Please sign in to comment.