From a281f7690da598964168f4100571cc39b93a8cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Thu, 20 May 2021 23:02:44 +0200 Subject: [PATCH] zpool: import: use realloc for realloc, remove strtok MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Brian Behlendorf Reviewed-by: John Kennedy Signed-off-by: Ahelenia ZiemiaƄska Closes #12094 --- cmd/zpool/zpool_main.c | 44 ++++++++++++------------------------------ cmd/zpool/zpool_util.c | 16 +++++++++++++++ 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 d7d93c4da343..887229d9a8b4 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 @@ zpool_do_import(int argc, char **argv) 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 1c1eb024f365..1c64c83d8ff1 100644 --- a/cmd/zpool/zpool_util.c +++ b/cmd/zpool/zpool_util.c @@ -49,6 +49,22 @@ safe_malloc(size_t size) return (data); } +/* + * 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. */ diff --git a/cmd/zpool/zpool_util.h b/cmd/zpool/zpool_util.h index abaa22d78c20..5557859ed48f 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);