diff options
author | наб <[email protected]> | 2021-05-20 23:02:44 +0200 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2021-05-26 14:50:59 -0700 |
commit | a281f7690da598964168f4100571cc39b93a8cab (patch) | |
tree | 9d0fca9917ba65ce1bb977cf706bdf267ccf5120 /cmd | |
parent | 31f4c8cb19bf3f7a1629e259af77d8a63f3816fd (diff) |
zpool: import: use realloc for realloc, remove strtok
Reviewed-by: Brian Behlendorf <[email protected]>
Reviewed-by: John Kennedy <[email protected]>
Signed-off-by: Ahelenia Ziemiańska <[email protected]>
Closes #12094
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/zpool/zpool_main.c | 44 | ||||
-rw-r--r-- | cmd/zpool/zpool_util.c | 16 | ||||
-rw-r--r-- | cmd/zpool/zpool_util.h | 1 |
3 files changed, 29 insertions, 32 deletions
diff --git a/cmd/zpool/zpool_main.c b/cmd/zpool/zpool_main.c index d7d93c4da..887229d9a 100644 --- a/cmd/zpool/zpool_main.c +++ b/cmd/zpool/zpool_main.c @@ -3499,16 +3499,8 @@ zpool_do_import(int argc, char **argv) cachefile = optarg; break; case 'd': - if (searchdirs == NULL) { - searchdirs = safe_malloc(sizeof (char *)); - } else { - char **tmp = safe_malloc((nsearch + 1) * - sizeof (char *)); - bcopy(searchdirs, tmp, nsearch * - sizeof (char *)); - free(searchdirs); - searchdirs = tmp; - } + searchdirs = safe_realloc(searchdirs, + (nsearch + 1) * sizeof (char *)); searchdirs[nsearch++] = optarg; break; case 'D': @@ -3698,24 +3690,16 @@ zpool_do_import(int argc, char **argv) * Check the environment for the preferred search path. */ if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { - char *dir; + char *dir, *tmp = NULL; envdup = strdup(env); - dir = strtok(envdup, ":"); - while (dir != NULL) { - if (searchdirs == NULL) { - searchdirs = safe_malloc(sizeof (char *)); - } else { - char **tmp = safe_malloc((nsearch + 1) * - sizeof (char *)); - bcopy(searchdirs, tmp, nsearch * - sizeof (char *)); - free(searchdirs); - searchdirs = tmp; - } + for (dir = strtok_r(envdup, ":", &tmp); + dir != NULL; + dir = strtok_r(NULL, ":", &tmp)) { + searchdirs = safe_realloc(searchdirs, + (nsearch + 1) * sizeof (char *)); searchdirs[nsearch++] = dir; - dir = strtok(NULL, ":"); } } @@ -3754,10 +3738,8 @@ zpool_do_import(int argc, char **argv) } if (err == 1) { - if (searchdirs != NULL) - free(searchdirs); - if (envdup != NULL) - free(envdup); + free(searchdirs); + free(envdup); nvlist_free(policy); nvlist_free(pools); nvlist_free(props); @@ -3795,10 +3777,8 @@ error: nvlist_free(props); nvlist_free(pools); nvlist_free(policy); - if (searchdirs != NULL) - free(searchdirs); - if (envdup != NULL) - free(envdup); + free(searchdirs); + free(envdup); return (err ? 1 : 0); } diff --git a/cmd/zpool/zpool_util.c b/cmd/zpool/zpool_util.c index 1c1eb024f..1c64c83d8 100644 --- a/cmd/zpool/zpool_util.c +++ b/cmd/zpool/zpool_util.c @@ -50,6 +50,22 @@ safe_malloc(size_t size) } /* + * Utility function to guarantee realloc() success. + */ +void * +safe_realloc(void *from, size_t size) +{ + void *data; + + if ((data = realloc(from, size)) == NULL) { + (void) fprintf(stderr, "internal error: out of memory\n"); + exit(1); + } + + return (data); +} + +/* * Display an out of memory error message and abort the current program. */ void diff --git a/cmd/zpool/zpool_util.h b/cmd/zpool/zpool_util.h index abaa22d78..5557859ed 100644 --- a/cmd/zpool/zpool_util.h +++ b/cmd/zpool/zpool_util.h @@ -39,6 +39,7 @@ extern "C" { * Basic utility functions */ void *safe_malloc(size_t); +void *safe_realloc(void *, size_t); void zpool_no_memory(void); uint_t num_logs(nvlist_t *nv); uint64_t array64_max(uint64_t array[], unsigned int len); |