]> git.proxmox.com Git - mirror_zfs.git/commitdiff
zpool: import: use realloc for realloc, remove strtok
authorнаб <nabijaczleweli@nabijaczleweli.xyz>
Thu, 20 May 2021 21:02:44 +0000 (23:02 +0200)
committerBrian Behlendorf <behlendorf1@llnl.gov>
Wed, 26 May 2021 21:50:59 +0000 (14:50 -0700)
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12094

cmd/zpool/zpool_main.c
cmd/zpool/zpool_util.c
cmd/zpool/zpool_util.h

index d7d93c4da3436553ba9701629f89f094cedf51eb..887229d9a8b49c994775c57e2b97aa3c187dd0a7 100644 (file)
@@ -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);
 }
index 1c1eb024f365c80fdd8a676811178abe844c676d..1c64c83d8ff15b8e3e61abbca1c3436b03e4d8d6 100644 (file)
@@ -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.
  */
index abaa22d78c20c070145912be83460e840a2502d8..5557859ed48fd4632f7041790ff87679cab292e2 100644 (file)
@@ -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);