libzutil: zfs_resolve_shortname: don't strdup() ZPOOL_IMPORT_PATH

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #13223
This commit is contained in:
наб 2022-03-15 23:23:53 +01:00 committed by Brian Behlendorf
parent 018937884f
commit 8a2ed86001
1 changed files with 20 additions and 19 deletions

View File

@ -56,35 +56,36 @@ zfs_dirnamelen(const char *path)
int int
zfs_resolve_shortname(const char *name, char *path, size_t len) zfs_resolve_shortname(const char *name, char *path, size_t len)
{ {
int i, error = -1; const char *env = getenv("ZPOOL_IMPORT_PATH");
char *dir, *env, *envdup, *tmp = NULL;
env = getenv("ZPOOL_IMPORT_PATH");
errno = ENOENT;
if (env) { if (env) {
envdup = strdup(env); for (;;) {
for (dir = strtok_r(envdup, ":", &tmp); env += strspn(env, ":");
dir != NULL && error != 0; size_t dirlen = strcspn(env, ":");
dir = strtok_r(NULL, ":", &tmp)) { if (dirlen) {
(void) snprintf(path, len, "%s/%s", dir, name); (void) snprintf(path, len, "%.*s/%s",
error = access(path, F_OK); (int)dirlen, env, name);
if (access(path, F_OK) == 0)
return (0);
env += dirlen;
} else
break;
} }
free(envdup);
} else { } else {
const char * const *zpool_default_import_path;
size_t count; 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 (size_t i = 0; i < count; ++i) {
for (i = 0; i < count && error < 0; i++) {
(void) snprintf(path, len, "%s/%s", (void) snprintf(path, len, "%s/%s",
zpool_default_import_path[i], name); 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);
} }
/* /*
@ -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; int path_len, cmp_len, i = 0, error = ENOENT;
char *dir, *env, *envdup = NULL, *tmp = NULL; char *dir, *env, *envdup = NULL, *tmp = NULL;
char path_name[MAXPATHLEN]; char path_name[MAXPATHLEN];
const char * const *zpool_default_import_path = NULL; const char *const *zpool_default_import_path = NULL;
size_t count; size_t count;
cmp_len = strlen(cmp_name); cmp_len = strlen(cmp_name);